我对这段代码有疑问,我的目标是通过 API 发送修改,所以我正在做request
over HttpClient
.
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
public class patchticket
{
public string patch(string ticketid)
{
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("PATCH"), "https://desk.zoho.com/api/v1/tickets/"+ticketid))
{
request.Headers.TryAddWithoutValidation("Authorization", "6af7d2d213a3ba5e9bc64b80e02b000");
request.Headers.TryAddWithoutValidation("OrgId", "671437200");
request.Content = new StringContent("{\"priority\" : \"High\"}", Encoding.UTF8, "application/x-www-form-urlencoded");
var response = httpClient.SendAsync(request);
return response
}
}
}
}
结果是我没有任何错误,但更改没有生效。
凭据没问题,我已经使用相同参数的curl对其进行了测试,效果很好。
看起来您想发布一个json
在请求中。尝试定义正确的内容类型application/json
。对于样品:
request.Content = new StringContent("{\"priority\" : \"High\"}",
Encoding.UTF8,
"application/json");
由于您的方法返回一个string
它可以是非异步方法。方法SendAsync
是异步的,您必须等待请求完成。你可以尝试打电话Result
请求后。对于样品:
var response = httpClient.SendAsync(request).Result;
return response.Content; // string content
你会得到一个对象Http响应消息 https://learn.microsoft.com/pt-br/dotnet/api/system.net.http.httpresponsemessage?view=netframework-4.8。关于其响应,有很多有用的信息。
无论如何,由于它是 IO 绑定操作,所以最好使用异步版本,如下所示:
var response = await httpClient.SendAsync(request);
return response.Content; // string content
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)