【发布时间】:2019-01-29 20:36:10
【问题描述】:
我正在尝试从我的 C# 代码在线访问我在 TFS 中的项目,以便使用 RESTapi 获取有关构建、任务、项目等的所有数据,我一直在遵循在线提供的文档来执行此操作( http://www.visualstudio.com/en-us/integrate/get-started/get-started-rest-basics-vsi),但是,当我想从 url 获取 Json 响应时,我总是得到:HTTP code 203: Non-Authoritative Information,因此我无法获取 Json 数据。如果我尝试使用 POSTMAN(chrome 扩展)获取响应,我会得到一个 HTTP 代码 200 和我需要的数据。
这是我的代码:
public static async void GetBuilds()
{
try
{
var username = "userTest";
var password = "PassTest";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", username, password))));
using (HttpResponseMessage response = client.GetAsync(
"https://myproject.visualstudio.com/DefaultCollection/_apis/build/builds?api-version=1.0-preview.1").Result)
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
我总是在响应中得到大量的 HTML,但没有接近我需要的,我做错了什么?
非常感谢您抽出宝贵时间。
【问题讨论】:
-
您是否尝试过运行他们的完整示例(如果需要,只需硬编码您的用户名/密码)?