我需要做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. NodeJS與異步請求
- 2. RestTemplate PATCH請求
- 3. 請求1.0.4異步請求
- 4. 異步請求
- 5. AJAX請求同步與異步
- 6. 來自線程與異步請求的NSURLConnection同步請求
- 7. REST API PATCH請求
- 8. nsurlconnection異步請求
- 9. MonoTouch異步請求
- 10. 異步JSON請求
- 11. JSON異步請求
- 12. 異步請求vaadin
- 13. Django異步請求
- 14. GuzzleHttp異步請求異常
- 15. updatePanel設置異步類型請求
- 16. 加載異步請求的其他類
- 17. 模型類中的ASIHTTPRequest異步請求
- 18. 異步請求與商店數據
- 19. 角 - 與請求攔截異步操作
- 20. Node.js與APDU的異步請求
- 21. 與ruby的異步HTTP請求
- 22. windows phone 8異步web請求與mvvm
- 23. 異步HTTP請求,排隊請求
- 24. 異步HTTP請求與新線程上的HTTP請求
- 25. SCIM 2.0的PATCH請求
- 26. 對PATCH請求的響應
- 27. 異步抽象HTTP請求
- 28. Yii異步jsonp請求
- 29. Ajax請求不是異步
- 30. ASP.NET異步請求回調
而不是 ''' HttpResponseMessage response = new HttpResponseMessage();使用 ''' var response = default(HttpResponseMessage); ''' – Wilmer 2017-05-04 10:34:06