c# - 如何使用 C# 代码从 teamcity 8.1.2 下载工件

标签 c# http download teamcity teamcity-8.0

我想使用 C# 代码从 teamcity 下载工件(zip 文件)。

我根据 TC 文档(https://confluence.jetbrains.com/display/TCD8/Accessing+Server+by+HTTP 和)编写了这段代码

string artifactSource = @"http://testuser:testpassword@teamcity.mydomain/httpAuth/downloadArtifacts.html?buildTypeId=ExampleBuildType&buildId=lastSuccessful";
using(WebClient teamcity = new WebClient())
{
  teamcity.DownloadFile(artifactSource, @"D:\Downloads\1.zip");
}

在 Visual Studio 中我得到: System.dll 中出现“System.Net.WebException”类型的未处理异常 附加信息:远程服务器返回错误:(401) 未经授权。

当我在浏览器中输入 url 时,我得到了正确的响应(文件已准备好下载)。我做错了什么?我应该以不同的方式进行授权吗?

最佳答案

以下代码实现了您所描述的内容:

var artifactSource = @"http://teamcity.mydomain/httpAuth/downloadArtifacts.html?buildTypeId=ExampleBuildType&buildId=lastSuccessful";

using (var teamcityRequest = new WebClient { Credentials = new NetworkCredential("username", "password") })
{
    teamcityRequest.DownloadFile(artifactSource, @"D:\Downloads\1.zip");
}

如您所见,我已取出用户名和密码并将它们传递给 WebClient 的 Credentials 属性。

如果您在 TeamCity 中启用了 guest 帐户(我在我的公司这样做),我还建议考虑 guest 身份验证。这允许您根本不使用任何凭据。在这种情况下,您需要将 url 中的“httpAuth”更改为“guestAuth”,代码变为

var artifactSource = @"http://teamcity.mydomain/guestAuth/downloadArtifacts.html?buildTypeId=ExampleBuildType&buildId=lastSuccessful";

using (var teamcityRequest = new WebClient())
{
    teamcityRequest.DownloadFile(artifactSource, @"D:\Downloads\1.zip");
}

希望对您有所帮助。

关于c# - 如何使用 C# 代码从 teamcity 8.1.2 下载工件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26405463/

相关文章:

c# - 使用 WIA 自动化扫描多页

c# - Windows 7 上的 log4net EventLogAppender 问题

html - C-HTTP网络服务器: how to cache

json - HTTP 数据包中可以返回的数量是否有限制?或者这是 WCF JSON 限制?

powershell - 使用 PowerShell FTP 下载多个文件

c# - Windows 10通用应用程序。如何触发后台任务?

c# - 如何每次在每个测试方法(包含在同一测试装置中)之前初始化实例变量(在 nunit 的测试装置中声明)?

javascript - 尝试从给定的 url 解析 JSON

java - Android DownloadManager - 下载 apk 并安装

PHP 文件服务脚本 : Unreliable Downloads?