如何在 dotnet 核心中使用 HttpClient 进行补丁请求?
How do I do a patch request using HttpClient in dotnet core?
我正在尝试使用 dotnet 核心中的 HttpClient
创建一个 Patch
请求。我找到了其他方法,
using (var client = new HttpClient())
{
client.GetAsync("/posts");
client.PostAsync("/posts", ...);
client.PutAsync("/posts", ...);
client.DeleteAsync("/posts");
}
但似乎找不到 Patch
选项。是否可以使用 HttpClient
执行 Patch
请求?如果是这样,有人可以告诉我如何做的例子吗?
感谢 Daniel A. White 的评论,我得到了以下工作。
using (var client = new HttpClient())
{
var request = new HttpRequestMessage(new HttpMethod("PATCH"), "your-api-endpoint");
try
{
response = await client.SendAsync(request);
}
catch (HttpRequestException ex)
{
// Failed
}
}
HttpClient 没有开箱即用的补丁。
简单地做这样的事情:
// more things here
using (var client = new HttpClient())
{
client.BaseAddress = hostUri;
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64Credentials);
var method = "PATCH";
var httpVerb = new HttpMethod(method);
var httpRequestMessage =
new HttpRequestMessage(httpVerb, path)
{
Content = stringContent
};
try
{
var response = await client.SendAsync(httpRequestMessage);
if (!response.IsSuccessStatusCode)
{
var responseCode = response.StatusCode;
var responseJson = await response.Content.ReadAsStringAsync();
throw new MyCustomException($"Unexpected http response {responseCode}: {responseJson}");
}
}
catch (Exception exception)
{
throw new MyCustomException($"Error patching {stringContent} in {path}", exception);
}
}
截至 2022 年 2 月
###原答案###
自 .Net Core 2.1 起,PatchAsync()
现在可用于 HttpClient
参考:
https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.patchasync
我正在尝试使用 dotnet 核心中的 HttpClient
创建一个 Patch
请求。我找到了其他方法,
using (var client = new HttpClient())
{
client.GetAsync("/posts");
client.PostAsync("/posts", ...);
client.PutAsync("/posts", ...);
client.DeleteAsync("/posts");
}
但似乎找不到 Patch
选项。是否可以使用 HttpClient
执行 Patch
请求?如果是这样,有人可以告诉我如何做的例子吗?
感谢 Daniel A. White 的评论,我得到了以下工作。
using (var client = new HttpClient())
{
var request = new HttpRequestMessage(new HttpMethod("PATCH"), "your-api-endpoint");
try
{
response = await client.SendAsync(request);
}
catch (HttpRequestException ex)
{
// Failed
}
}
HttpClient 没有开箱即用的补丁。 简单地做这样的事情:
// more things here
using (var client = new HttpClient())
{
client.BaseAddress = hostUri;
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64Credentials);
var method = "PATCH";
var httpVerb = new HttpMethod(method);
var httpRequestMessage =
new HttpRequestMessage(httpVerb, path)
{
Content = stringContent
};
try
{
var response = await client.SendAsync(httpRequestMessage);
if (!response.IsSuccessStatusCode)
{
var responseCode = response.StatusCode;
var responseJson = await response.Content.ReadAsStringAsync();
throw new MyCustomException($"Unexpected http response {responseCode}: {responseJson}");
}
}
catch (Exception exception)
{
throw new MyCustomException($"Error patching {stringContent} in {path}", exception);
}
}
截至 2022 年 2 月
###原答案###
自 .Net Core 2.1 起,PatchAsync()
现在可用于 HttpClient
参考: https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.patchasync