gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:35:17 25 4
gpt4 key购买 nike

我想使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com