【发布时间】:2018-03-14 18:10:32
【问题描述】:
我正在使用 TFS2018 并像这样调用构建 api
internal void UpdateSourceBranches(List<BuildDefinition> defs)
{
using (var handler = new HttpClientHandler { Credentials = new NetworkCredential(tfsUser, tfsPass) })
using (var client = new HttpClient(handler))
{
try
{
client.BaseAddress = new Uri(tfsServer);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
foreach (var def in defs)
{
var buildId = def.Id;
var sourceBranch = $"$/{def.Repository.Name}/{def.Project.Name}";
var parameters = new Dictionary<string, string> { { "BuildConfiguration", "release" },
{ "BuildPlatform", "x86|x64|ARM" },
{ "system.debug", "true" }
};
var jsonParams = JsonConvert.SerializeObject(parameters);
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("id", buildId.ToString()),
new KeyValuePair<string, string>("sourceBranch", sourceBranch),
new KeyValuePair<string, string>("parameters", jsonParams)
});
var response = client.PostAsync($"DefaultCollection/{def.Repository.Name}/_apis/build/builds?api-version=3.0-preview.1", content);
var s = response.Result;
}
}
catch (Exception ex)
{
}
}
}
但得到以下响应
{StatusCode: 405, ReasonPhrase: '方法不允许', 版本: 1.1, 内容:System.Net.Http.StreamContent,标头:{ Pragma: no-cache X-TFS-ProcessId: ActivityId: X-TFS-Session: X-VSS-E2EID: X-FRAME-OPTIONS: SAMEORIGIN X-VSS-UserData: :user Persistent-Auth: true Lfs-Authenticate: NTLM X-Content-Type-Options: nosniff 缓存控制:无缓存日期:2018 年 3 月 9 日星期五 14:37:16 GMT P3P: CP="CAO DSP COR ADMa DEV CONo TELo CUR PSA PSD TAI IVDo OUR SAMi BUS DEM NAV STA UNI COM INT PHY ONL FIN PUR LOC CNT" 服务器: Microsoft-IIS/10.0 X-AspNet-版本:4.0.30319 X-Powered-By:ASP.NET 内容长度:93 允许:GET 内容类型:应用程序/json; charset=utf-8 过期:-1 }}
知道为什么我得到 Method Not Allowed 吗?
这是不好的回应:
{StatusCode: 400, ReasonPhrase: 'Bad Request', 版本: 1.1, 内容: System.Net.Http.StreamContent,标头:{ Pragma: no-cache
X-TFS-ProcessId:xxxxxx-xxxxx-xxxxx-xxxxxx-xxxxxxxxxx ActivityId: xxxxxx-xxxxx-xxxxx-xxxxxx-xxxxxxxxxx X-TFS-会话: xxxxxx-xxxxx-xxxxx-xxxxxx-xxxxxxxxxx X-VSS-E2EID: xxxxxx-xxxxx-xxxxx-xxxxxx-xxxxxxxxxx X-FRAME-OPTIONS: SAMEORIGIN
X-VSS-UserData: xxxxxx-xxxxx-xxxxx-xxxxxx-xxxxxxxxxx:user
Persistent-Auth: true Lfs-Authenticate: NTLM
X-Content-Type-Options:nosniff 缓存控制:无缓存日期:星期二, 2018 年 3 月 13 日 09:21:53 GMT P3P:CP="...多个关键字" 服务器: Microsoft-IIS/10.0 X-AspNet-版本:4.0.30319 X-Powered-By: ASP.NET 内容长度:547 内容类型:应用程序/json; charset=utf-8 过期:-1 }}
【问题讨论】:
标签: tfs azure-devops