VSTS REST api在返回json时返回xml

时间:2018-06-06 13:13:53

标签: c# rest azure-devops httpwebrequest azure-devops-rest-api

class RestClient
    { public string endPoint { get; set; }

        public RestClient(string e)
        {
            endPoint = e;
        }
        public string makeRequest()
        {
            string strResponse = string.Empty;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);
            request.Accept = "application/json";
            request.Credentials = HeaderTool.nCred;
            request.Method = "GET";

            using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    Console.WriteLine("Status code not ok");
                }
                //Process the response stream
                using (Stream responseStream = response.GetResponseStream())
                {
                    if (responseStream != null)
                    {
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            strResponse = reader.ReadToEnd();
                        }
                    }
                }//End of responseStream scope
            }

            return strResponse;
        }//End of response scope
    }

所以我正在查看我的项目在TFS上的提交,后缀为/commits?searchCriteria.includeWorkItems=true&api-version=4.1 哪个有效 但是,无论我做什么,当我在控制台中打印输出时,它都是XML。我已经将HttpWebRequest的Accept字段设置为application / json,但它什么都没改变 我甚至尝试将.json附加到uri

我想要这种输出 {     “commitId”:“SHA”,     “作者”:{         “名字”:“Bill Nye the Science Guy”,         “email”:“gmail@gmail.com”,         “日期”:“2018”     } } 你得到了它的正义 非常感谢任何帮助!

First part of output:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">






<html lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head><title>

            Microsoft Team Foundation Server

</title><meta http-equiv="X-UA-Compatible" content="IE=11;&#32;IE=10;&#32;IE=9;&#32;IE=8" /><meta name="msapplication-config" content="none" />
    <link rel="SHORTCUT ICON" href="/tfs/favicon.ico"/>

1 个答案:

答案 0 :(得分:0)

它没有返回XML。它正在返回 HTML 。这几乎可以肯定是因为您被重定向到登录页面。您正被重定向到登录页面,因为您没有正确进行身份验证。

我不知道HeaderTool.nCred的价值,但这几乎肯定不正确。您应该使用个人身份验证令牌或OAuth令牌来对VSTS进行身份验证。

您可以使用client libraries来处理正确的身份验证。我建议你自己动手实施它们。

您还可以通过标准HTTP客户端库将PAT传递给authenticate

适用于您的方案的相关代码段:

public static async void GetBuilds()
{
    try
    {
        var personalaccesstoken = "PATFROMWEB";

        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}", "", personalaccesstoken))));

            using (HttpResponseMessage response = client.GetAsync(
                        "https://{account}.visualstudio.com/DefaultCollection/_apis/build-release/builds").Result)
            {
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseBody);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

请注意,PAT正在进行BASE64编码,并通过身份验证标头传入。