我使用REST API使用REST API documentation创建工作项。为此,我需要使用Patch请求,但此代码无效。程序以代码0(0x0)退出。
HttpClientHandler httpClientHandler = new HttpClientHandler();
using (HttpClient client = new HttpClient(httpClientHandler))
{
var content = "[{'op': 'add','path': '/fields/System.Title', 'value': 'Title' }]";
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
string URLTest = "https://MyProject.visualstudio.com/DefaultCollection/ProjectName/_apis/wit/workitems/$Task?api-version=2.0";
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "*******", "******"))));
var method = new HttpMethod("PATCH");
var request = new HttpRequestMessage(method, URLTest)
{
Content = new StringContent(content, Encoding.UTF8,
"application/json-patch+json")
};
HttpResponseMessage response = await client.SendAsync(request);
答案 0 :(得分:0)
0 (0x0)
这只是调试消息。您可以通过右键单击输出窗口并取消选中线程结束消息来关闭它。
使用uriBuilder构建uri必须工作
var uriBuilder = new UriBuilder(URLTest);
uriBuilder.Scheme = "http";
var request = new HttpRequestMessage(method, uriBuilder.Uri)
{
Content = new StringContent(content, Encoding.UTF8,
"application/json-patch+json")
};
更改asyc方法并找到错误消息
HttpResponseMessage response = client.Send(request);
//现在您将收到错误消息
否则使用try catch
阻止
try
{
HttpResponseMessage response = await client.SendAsync(request);
}
catch (TaskCanceledException e)
{
Debug.WriteLine("ERROR: " + e.ToString());
}