Body 的 c# HttpClient 补丁请求?
c# HttpClient Patch request with Body?
我正在尝试使用 http 客户端发送请求。我需要将 body 添加到请求中并传入参数。我有一些基于 http 请求的代码,但目前无法正常工作:
httpClient.BaseAddress = new Uri(url);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
var value = JsonConvert.SerializeObject(obj);
var content = new StringContent(value, Encoding.UTF8, "application/json");
**var request = new HttpRequestMessage(new HttpMethod("PATCH"), url + value);**
var response = await httpClient.SendAsync(request);
string Path = response.RequestMessage.RequestUri.AbsolutePath.ToString();
Success = response.IsSuccessStatusCode;
我的问题在代码的粗体部分。我需要能够将 body 部分传递给请求。
当前回复:
{StatusCode:404,ReasonPhrase:'Not Found',版本:1.1,内容:System.Net.Http.StreamContent,Headers:
{
Transfer-Encoding:分块
日期:2022 年 3 月 29 日,星期二 18:22:37 GMT
服务器:
}}
到目前为止,无法找到 body 的 PATCH 的工作代码片段。我什至尝试过 POST.
我也试过这个:
//httpClient.BaseAddress = new Uri(url);
//httpClient.DefaultRequestHeaders.Accept.Clear();
//httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//var s = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };
//var content = JsonConvert.SerializeObject(obj, s);
//var request = new HttpRequestMessage(new HttpMethod("PATCH"), url);
//request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
//var response = await httpClient.SendAsync(request);
//var responseString = await response.Content.ReadAsStringAsync();
//string Path = response.RequestMessage.RequestUri.AbsolutePath.ToString();
但是,我注意到我 运行 遇到了和以前一样的问题。它没有将 body 添加到请求中。我 nuil 它但从未通过 body。所以我需要在请求中传递body。
您需要做的就是设置 Content
属性 的 HttpRequestMessage
:
var request = new HttpRequestMessage(HttpMethod.Patch, url) {
Content = new StringContent(value, Encoding.UTF8, "application/json")
};
我正在尝试使用 http 客户端发送请求。我需要将 body 添加到请求中并传入参数。我有一些基于 http 请求的代码,但目前无法正常工作:
httpClient.BaseAddress = new Uri(url);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
var value = JsonConvert.SerializeObject(obj);
var content = new StringContent(value, Encoding.UTF8, "application/json");
**var request = new HttpRequestMessage(new HttpMethod("PATCH"), url + value);**
var response = await httpClient.SendAsync(request);
string Path = response.RequestMessage.RequestUri.AbsolutePath.ToString();
Success = response.IsSuccessStatusCode;
我的问题在代码的粗体部分。我需要能够将 body 部分传递给请求。
当前回复: {StatusCode:404,ReasonPhrase:'Not Found',版本:1.1,内容:System.Net.Http.StreamContent,Headers: { Transfer-Encoding:分块 日期:2022 年 3 月 29 日,星期二 18:22:37 GMT 服务器: }}
到目前为止,无法找到 body 的 PATCH 的工作代码片段。我什至尝试过 POST.
我也试过这个:
//httpClient.BaseAddress = new Uri(url);
//httpClient.DefaultRequestHeaders.Accept.Clear();
//httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//var s = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };
//var content = JsonConvert.SerializeObject(obj, s);
//var request = new HttpRequestMessage(new HttpMethod("PATCH"), url);
//request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
//var response = await httpClient.SendAsync(request);
//var responseString = await response.Content.ReadAsStringAsync();
//string Path = response.RequestMessage.RequestUri.AbsolutePath.ToString();
但是,我注意到我 运行 遇到了和以前一样的问题。它没有将 body 添加到请求中。我 nuil 它但从未通过 body。所以我需要在请求中传递body。
您需要做的就是设置 Content
属性 的 HttpRequestMessage
:
var request = new HttpRequestMessage(HttpMethod.Patch, url) {
Content = new StringContent(value, Encoding.UTF8, "application/json")
};