2014-10-06 36 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」);