PATCH Async requests with Windows.Web.Http.HttpClient class

后端 未结 4 1686
花落未央
花落未央 2020-11-29 03:31

I need to do a PATCH request with the Windows.Web.Http.HttpClient class and there is no official documentation on how to do it. How can I do this?<

4条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 03:50

    I found how to do a "custom" PATCH request with the previous System.Net.Http.HttpClient class here, and then fiddled with until I made it work in the Windows.Web.Http.HttpClient class, like so:

    public async Task 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;
    }
    

提交回复
热议问题