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

后端 未结 4 1682
花落未央
花落未央 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:52

    I'd like to extend on @alexander-pacha's answer and suggest adding following extension class somewhere in a common library. Wether this be a common library for a project / client / framework/... is something you'll have to make out on your own.

        public static class HttpClientExtensions
        {
            /// 
            /// Send a PATCH request to the specified Uri as an asynchronous operation.
            /// 
            /// 
            /// 
            /// Returns .The task object representing the asynchronous operation.
            /// 
            /// The instantiated Http Client 
            /// The Uri the request is sent to.
            /// The HTTP request content sent to the server.
            /// The  was null.
            /// The  was null.
            public static Task PatchAsync(this HttpClient client, string requestUri, HttpContent content)
            {
                return client.PatchAsync(CreateUri(requestUri), content);
            }
    
            /// 
            /// Send a PATCH request to the specified Uri as an asynchronous operation.
            /// 
            /// 
            /// 
            /// Returns .The task object representing the asynchronous operation.
            /// 
            /// The instantiated Http Client 
            /// The Uri the request is sent to.
            /// The HTTP request content sent to the server.
            /// The  was null.
            /// The  was null.
            public static Task PatchAsync(this HttpClient client, Uri requestUri, HttpContent content)
            {
                return client.PatchAsync(requestUri, content, CancellationToken.None);
            }
            /// 
            /// Send a PATCH request with a cancellation token as an asynchronous operation.
            /// 
            /// 
            /// 
            /// Returns .The task object representing the asynchronous operation.
            /// 
            /// The instantiated Http Client 
            /// The Uri the request is sent to.
            /// The HTTP request content sent to the server.
            /// A cancellation token that can be used by other objects or threads to receive notice of cancellation.
            /// The  was null.
            /// The  was null.
            public static Task PatchAsync(this HttpClient client, string requestUri, HttpContent content, CancellationToken cancellationToken)
            {
                return client.PatchAsync(CreateUri(requestUri), content, cancellationToken);
            }
    
            /// 
            /// Send a PATCH request with a cancellation token as an asynchronous operation.
            /// 
            /// 
            /// 
            /// Returns .The task object representing the asynchronous operation.
            /// 
            /// The instantiated Http Client 
            /// The Uri the request is sent to.
            /// The HTTP request content sent to the server.
            /// A cancellation token that can be used by other objects or threads to receive notice of cancellation.
            /// The  was null.
            /// The  was null.
            public static Task 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);
            }
        }
    

    This way you're not awaiting and holding up execution in some static extension class, but you handle that as if you were really doing a PostAsync or a PutAsync call. You also have the same overloads at your disposal and you're letting the HttpClient handle everything it was designed to handle.

提交回复
热议问题