【问题标题】:"Response status code does not indicate success: 500 (Internal Server Error)" while creating Test Suite through TFS Rest API“响应状态代码不表示成功:500(内部服务器错误)”通过 TFS Rest API 创建测试套件时
【发布时间】:2017-11-24 12:25:40
【问题描述】:

尝试使用TFS 2017 REST API 创建Test Suite 时,出现错误:

System.Net.Http.HttpRequestException - 响应状态代码不 表示成功:500(内部服务器错误)

我试过的代码:

using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    string base64StringPat = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", Configs.Pat)));
    AuthenticationHeaderValue authHeader = new AuthenticationHeaderValue("Basic", base64StringPat);
    client.DefaultRequestHeaders.Authorization = authHeader;

    string url = "http://vmctp-tl-mtm:8080/tfs/DefaultCollection/SgkProject/_apis/test/Plans/7/Suites/8?api-version=1.0";
    var content = new StringContent("{\"suiteType\":\"StaticTestSuite\",\"name\":\"Module1\"}", Encoding.UTF8, "application/json");
    using (HttpResponseMessage response = client.PostAsync(url, content).Result)
    {
        response.EnsureSuccessStatusCode();
        string responseBody = await response.Content.ReadAsStringAsync();
        Console.WriteLine(responseBody);
    }
}

我已使用 Microsoft 的此文档来调用 API:Create a test suite

请指导我解决问题。

【问题讨论】:

    标签: tfs azure-devops microsoft-test-manager


    【解决方案1】:

    HTTP 代码 500 表示这是您的服务器上的错误。服务器在尝试处理此 POST 请求时抛出异常。

    所以,这个错误与HttpClient 无关。只需先检查您的服务器并查看导致异常的原因。

    可能是服务器不期望指定的content type。发布StringContent 会将内容类型设置为text/plain。您可能会发现服务器不喜欢那样。在这种情况下,只需尝试找出服务器期望的媒体类型并设置StringContent 实例的Headers.ContentType

    无论如何,我可以通过以下示例创建套件,您可以尝试一下:

    using System;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CreateTestSuite
    {
        class Program
        {
            public static void Main()
            {
                Task t = CreateTestSuite();
                Task.WaitAll(new Task[] { t });
            }
            private static async Task CreateTestSuite()
            {
                try
                {
                    var username = "username";
                    var password = "password";
    
                    using (var 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))));
    
                        string url = "http://server:8080/tfs/DefaultCollection/LCScrum/_apis/test/plans/212/suites/408?api-version=1.0";
                        var content = new StringContent("{\"suiteType\":\"StaticTestSuite\",\"name\":\"Module3\"}", Encoding.UTF8, "application/json");
                        using (HttpResponseMessage response = client.PostAsync(url, content).Result)
                        {
                            response.EnsureSuccessStatusCode();
                            string responseBody = await response.Content.ReadAsStringAsync();
                            Console.WriteLine(responseBody);
                        }
    
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-30
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-07
      • 1970-01-01
      相关资源
      最近更新 更多