我需要做PATCH
Windows.Web.Http.HttpClient
类请求,并没有关于如何做到这一点的官方文档。我怎样才能做到这一点?PATCH异步请求与Windows.Web.Http.HttpClient类
回答
我发现了如何做一个“自定义” PATCH
请求与之前System.Net.Http.HttpClient
类here,然后摆弄直到我把它在Windows.Web.Http.HttpClient
类的工作,像这样:
public async Task<HttpResponseMessage> PatchAsync(HttpClient client, Uri requestUri, IHttpContent iContent) {
var method = new HttpMethod("PATCH");
var request = new HttpRequestMessage(method, requestUri) {
Content = iContent
};
HttpResponseMessage response = new HttpResponseMessage();
// In case you want to set a timeout
//CancellationToken cancellationToken = new CancellationTokenSource(60).Token;
try {
response = await client.SendRequestAsync(request);
// If you want to use the timeout you set
//response = await client.SendRequestAsync(request).AsTask(cancellationToken);
} catch(TaskCanceledException e) {
Debug.WriteLine("ERROR: " + e.ToString());
}
return response;
}
你可以写同样的方法扩展方法,这样你就可以直接调用它的HttpClient对象上:
public static class HttpClientExtensions
{
public static async Task<HttpResponseMessage> PatchAsync(this HttpClient client, Uri requestUri, HttpContent iContent)
{
var method = new HttpMethod("PATCH");
var request = new HttpRequestMessage(method, requestUri)
{
Content = iContent
};
HttpResponseMessage response = new HttpResponseMessage();
try
{
response = await client.SendAsync(request);
}
catch (TaskCanceledException e)
{
Debug.WriteLine("ERROR: " + e.ToString());
}
return response;
}
}
用法:
var responseMessage = await httpClient.PatchAsync(new Uri("testUri"), httpContent);
你如何传递内容? – 2015-05-25 15:14:41
你看到第二个参数?尝试像这样:'HttpContent httpContent = new StringContent(“Your JSON-String”,Encoding.UTF8,“application/json”);'用于String-Contents。 – 2015-05-25 15:16:50
纠正我,如果我错了,但PATCH方法意味着你只修改JSON内的特定数据。您如何修改产品的名称?如果通过“你的JSON字符串”你的意思是整个JSON,那么我很困惑。我尝试添加一个单独的属性,例如'HttpContent content = new StringContent(“{\”name \“:\”John Doe \“”,Encoding.UTF8,“application/json”);'但不添加内容请求。 – Caloyski 2017-06-13 17:13:05
我想扩展@ alexander-pacha的回答,并建议在共同库中的某处添加以下扩展类。这是一个项目/客户/框架/共同的图书馆... ...是你必须自己制定的。
public static class HttpClientExtensions
{
/// <summary>
/// Send a PATCH request to the specified Uri as an asynchronous operation.
/// </summary>
///
/// <returns>
/// Returns <see cref="T:System.Threading.Tasks.Task`1"/>.The task object representing the asynchronous operation.
/// </returns>
/// <param name="client">The instantiated Http Client <see cref="HttpClient"/></param>
/// <param name="requestUri">The Uri the request is sent to.</param>
/// <param name="content">The HTTP request content sent to the server.</param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="client"/> was null.</exception>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="requestUri"/> was null.</exception>
public static Task<HttpResponseMessage> PatchAsync(this HttpClient client, string requestUri, HttpContent content)
{
return client.PatchAsync(CreateUri(requestUri), content);
}
/// <summary>
/// Send a PATCH request to the specified Uri as an asynchronous operation.
/// </summary>
///
/// <returns>
/// Returns <see cref="T:System.Threading.Tasks.Task`1"/>.The task object representing the asynchronous operation.
/// </returns>
/// <param name="client">The instantiated Http Client <see cref="HttpClient"/></param>
/// <param name="requestUri">The Uri the request is sent to.</param>
/// <param name="content">The HTTP request content sent to the server.</param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="client"/> was null.</exception>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="requestUri"/> was null.</exception>
public static Task<HttpResponseMessage> PatchAsync(this HttpClient client, Uri requestUri, HttpContent content)
{
return client.PatchAsync(requestUri, content, CancellationToken.None);
}
/// <summary>
/// Send a PATCH request with a cancellation token as an asynchronous operation.
/// </summary>
///
/// <returns>
/// Returns <see cref="T:System.Threading.Tasks.Task`1"/>.The task object representing the asynchronous operation.
/// </returns>
/// <param name="client">The instantiated Http Client <see cref="HttpClient"/></param>
/// <param name="requestUri">The Uri the request is sent to.</param>
/// <param name="content">The HTTP request content sent to the server.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="client"/> was null.</exception>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="requestUri"/> was null.</exception>
public static Task<HttpResponseMessage> PatchAsync(this HttpClient client, string requestUri, HttpContent content, CancellationToken cancellationToken)
{
return client.PatchAsync(CreateUri(requestUri), content, cancellationToken);
}
/// <summary>
/// Send a PATCH request with a cancellation token as an asynchronous operation.
/// </summary>
///
/// <returns>
/// Returns <see cref="T:System.Threading.Tasks.Task`1"/>.The task object representing the asynchronous operation.
/// </returns>
/// <param name="client">The instantiated Http Client <see cref="HttpClient"/></param>
/// <param name="requestUri">The Uri the request is sent to.</param>
/// <param name="content">The HTTP request content sent to the server.</param>
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="client"/> was null.</exception>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="requestUri"/> was null.</exception>
public static Task<HttpResponseMessage> PatchAsync(this HttpClient client, Uri requestUri, HttpContent content, CancellationToken cancellationToken)
{
return client.SendAsync(new HttpRequestMessage(new HttpMethod("PATCH"), requestUri)
{
Content = content
}, cancellationToken);
}
private static Uri CreateUri(string uri)
{
return string.IsNullOrEmpty(uri) ? null : new Uri(uri, UriKind.RelativeOrAbsolute);
}
}
这样,你不等待,并在一些静态的扩展类举行了执行,但你处理,你是真的做一个PostAsync或PutAsync呼叫仿佛。您也可以使用相同的重载程序,并让HttpClient处理它旨在处理的所有内容。
这看起来不错。您应该考虑在.NET Framework的官方存储库中的Github上创建一个Pull-Request,因为他们欢迎贡献:https://github.com/dotnet/corefx/blob/master/src/System.Net.Http /src/System/Net/Http/HttpClient.cs – 2018-01-24 16:13:46
编辑:有人殴打我,它已被添加到您与其他人链接的回购站。 – 2018-01-26 13:42:26
对于它的工作,你需要通过这样的内容:
HttpContent httpContent =新的StringContent( “你的JSON字符串”,Encoding.UTF8, “应用/ JSON-补丁+ JSON”);
- 1. RestTemplate PATCH请求
- 2. AJAX请求同步与异步
- 3. 异步请求vaadin
- 4. nsurlconnection异步请求
- 5. Django异步请求
- 6. GuzzleHttp异步请求异常
- 7. 与ruby的异步HTTP请求
- 8. Node.js与APDU的异步请求
- 9. 异步请求与商店数据
- 10. 角 - 与请求拦截异步操作
- 11. 异步HTTP请求,排队请求
- 12. 异步HTTP请求与新线程上的HTTP请求
- 13. 对PATCH请求的响应
- 14. 多个异步URL请求
- 15. 角度异步http请求
- 16. c#异步http请求
- 17. Objective-C异步http请求
- 18. 异步http请求服务
- 19. 的UITableView和异步请求
- 20. rxjs:链接异步请求
- 21. 异步跨域GET请求
- 22. Delphi tcpServer异步请求
- 23. 异步json请求Objective C
- 24. 封装异步请求
- 25. Ajax请求不是异步
- 26. ASP.NET异步请求回调
- 27. Yii框架异步请求
- 28. 角异步查询请求
- 29. Yii异步jsonp请求
- 30. 取消webclient异步请求
而不是 ''' HttpResponseMessage response = new HttpResponseMessage();使用 ''' var response = default(HttpResponseMessage); ''' – Wilmer 2017-05-04 10:34:06