2014-10-06 85 views

回答

39

我发现了如何做一个“自定义” PATCH请求与之前System.Net.Http.HttpClienthere,然后摆弄直到我把它在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; 
} 
+0

而不是 ''' HttpResponseMessage response = new HttpResponseMessage();使用 ''' var response = default(HttpResponseMessage); ''' – Wilmer 2017-05-04 10:34:06

29

你可以写同样的方法扩展方法,这样你就可以直接调用它的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); 
+0

你如何传递内容? – 2015-05-25 15:14:41

+4

你看到第二个参数?尝试像这样:'HttpContent httpContent = new StringContent(“Your JSON-String”,Encoding.UTF8,“application/json”);'用于String-Contents。 – 2015-05-25 15:16:50

+0

纠正我,如果我错了,但PATCH方法意味着你只修改JSON内的特定数据。您如何修改产品的名称?如果通过“你的JSON字符串”你的意思是整个JSON,那么我很困惑。我尝试添加一个单独的属性,例如'HttpContent content = new StringContent(“{\”name \“:\”John Doe \“”,Encoding.UTF8,“application/json”);'但不添加内容请求。 – Caloyski 2017-06-13 17:13:05

10

我想扩展@ 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处理它旨在处理的所有内容。

+0

这看起来不错。您应该考虑在.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

+0

编辑:有人殴打我,它已被添加到您与其他人链接的回购站。 – 2018-01-26 13:42:26

0

对于它的工作,你需要通过这样的内容:

HttpContent httpContent =新的StringContent( “你的JSON字符串”,Encoding.UTF8, “应用/ JSON-补丁+ JSON”);