【问题标题】:Using RESTapi to connect to TFS online from code使用 REST api 从代码在线连接到 TFS
【发布时间】: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,但没有接近我需要的,我做错了什么?

非常感谢您抽出宝贵时间。

【问题讨论】:

  • 您是否尝试过运行他们的完整示例(如果需要,只需硬编码您的用户名/密码)?

标签: c# .net json tfs


【解决方案1】:

您的代码对我来说似乎是正确的。您是否为您的 VSO 帐户启用了备用凭据?没有它就行不通。这是 link 解释如何做到这一点。

您还可以在 codeplex 上查看我的项目:https://vsorest.codeplex.com/ 它展示了如何使用 C# 使用一些 VSO REST API

【讨论】:

  • 你说得对,我必须启用备用凭据,现在我正在获取数据,非常感谢您的帮助和分享您的项目!
猜你喜欢
  • 1970-01-01
  • 2016-09-10
  • 2021-05-21
  • 1970-01-01
  • 2017-10-25
  • 1970-01-01
  • 2017-05-31
  • 1970-01-01
  • 2017-09-12
相关资源
最近更新 更多