gpt4 book ai didi

tfs - 通过 TFS Rest API 创建测试套件时出现 "Response status code does not indicate success: 500 (Internal Server Error)"

转载 作者:行者123 更新时间:2023-12-05 01:16:11 26 4
gpt4 key购买 nike

在尝试创建 Test Suite 时使用 TFS 2017 REST API ,我收到错误:

System.Net.Http.HttpRequestException - Response status code does not indicate success: 500 (Internal Server Error)



我试过的代码:
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

请指导我解决问题。

最佳答案

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

所以,这个错误与HttpClient无关。 .只需先检查您的服务器,看看是什么导致了异常。

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

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

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());
}
}
}
}

关于tfs - 通过 TFS Rest API 创建测试套件时出现 "Response status code does not indicate success: 500 (Internal Server Error)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47473305/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com