// // HttpAsyncMethodExtensions v1.0 // http://bendewey.wordpress.com/2009/05/20/httpclient-send-async-convenience-extension/ // // Public Domain. // NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. // // Basic Usage: // var client = new HttpClient(); // client.GetAsync("http://www.example.com", (sender, eventArgs) => // { // var html = eventArgs.Response.Content.ReadAsString(); // }); using System; using System.Collections.Generic; using Microsoft.Http; using Microsoft.Http.Headers; namespace Microsoft.Http { public static class HttpAsyncMethodExtensions { #region DeleteAsync Extensions public static void DeleteAsync(this HttpClient client, string uri, EventHandler completed) { AsyncMethod(client, HttpMethod.DELETE, uri, completed); } public static void DeleteAsync(this HttpClient client, Uri uri, EventHandler completed) { AsyncMethod(client, HttpMethod.DELETE, uri, completed); } #endregion #region GetAsync Extensions public static void GetAsync(this HttpClient client, string uri, EventHandler completed) { AsyncMethod(client, HttpMethod.GET, uri, completed); } public static void GetAsync(this HttpClient client, Uri uri, EventHandler completed) { AsyncMethod(client, HttpMethod.GET, uri, completed); } public static void GetAsync(this HttpClient client, Uri uri, HttpQueryString queryString, EventHandler completed) { CheckNull(uri, "uri"); CheckNull(queryString, "queryString"); uri = HttpQueryString.MakeQueryString(uri, queryString); AsyncMethod(client, HttpMethod.GET, uri, completed); } public static void GetAsync(this HttpClient client, Uri uri, IEnumerable> queryString, EventHandler completed) { CheckNull(uri, "uri"); CheckNull(queryString, "queryString"); uri = HttpQueryString.MakeQueryString(uri, queryString); AsyncMethod(client, HttpMethod.GET, uri, completed); } #endregion #region HeadAsync Extensions public static void HeadAsync(this HttpClient client, string uri, EventHandler completed) { AsyncMethod(client, HttpMethod.HEAD, uri, completed); } public static void HeadAsync(this HttpClient client, Uri uri, EventHandler completed) { AsyncMethod(client, HttpMethod.HEAD, uri, completed); } #endregion #region PostAsync Extensions public static void PostAsync(this HttpClient client, string uri, HttpContent body, EventHandler completed) { AsyncMethod(client, HttpMethod.POST, uri, body, completed); } public static void PostAsync(this HttpClient client, Uri uri, HttpContent body, EventHandler completed) { AsyncMethod(client, HttpMethod.POST, uri, body, completed); } public static void PostAsync(this HttpClient client, string uri, string contentType, HttpContent body, EventHandler completed) { CheckNull(contentType, "contentType"); AsyncMethod(client, HttpMethod.POST, new Uri(uri, UriKind.RelativeOrAbsolute), contentType, body, completed); } public static void PostAsync(this HttpClient client, Uri uri, string contentType, HttpContent body, EventHandler completed) { CheckNull(contentType, "contentType"); AsyncMethod(client, HttpMethod.POST, uri, contentType, body, completed); } #endregion #region PutAsync Extensions public static void PutAsync(this HttpClient client, string uri, HttpContent body, EventHandler completed) { AsyncMethod(client, HttpMethod.PUT, uri, body, completed); } public static void PutAsync(this HttpClient client, Uri uri, HttpContent body, EventHandler completed) { AsyncMethod(client, HttpMethod.PUT, uri, body, completed); } public static void PutAsync(this HttpClient client, string uri, string contentType, HttpContent body, EventHandler completed) { CheckNull(contentType, "contentType"); AsyncMethod(client, HttpMethod.PUT, new Uri(uri, UriKind.RelativeOrAbsolute), contentType, body, completed); } public static void PutAsync(this HttpClient client, Uri uri, string contentType, HttpContent body, EventHandler completed) { CheckNull(contentType, "contentType"); AsyncMethod(client, HttpMethod.PUT, uri, contentType, body, completed); } #endregion #region private HttpMethod SendAsync proxies private static void AsyncMethod(HttpClient client, HttpMethod method, string uri, EventHandler completed) { CheckNull(uri, "uri"); AsyncMethod(client, method, new Uri(uri, UriKind.RelativeOrAbsolute), completed); } private static void AsyncMethod(HttpClient client, HttpMethod method, Uri uri, EventHandler completed) { CheckNull(client, "client"); CheckNull(uri, "uri"); client.SendAsync(method, uri, completed); } private static void AsyncMethod(HttpClient client, HttpMethod method, string uri, HttpContent body, EventHandler completed) { CheckNull(uri, "uri"); AsyncMethod(client, method, new Uri(uri, UriKind.RelativeOrAbsolute), body, completed); } private static void AsyncMethod(HttpClient client, HttpMethod method, Uri uri, HttpContent body, EventHandler completed) { CheckNull(client, "client"); CheckNull(uri, "uri"); CheckNull(body, "body"); CheckNull(completed, "completed"); client.SendAsync(method, uri, body, completed); } private static void AsyncMethod(HttpClient client, HttpMethod method, Uri uri, string contentType, HttpContent body, EventHandler completed) { CheckNull(client, "client"); CheckNull(uri, "uri"); CheckNull(body, "body"); CheckNull(body, "contentType"); var headers = new RequestHeaders { ContentType = contentType }; client.SendAsync(method, uri, headers, body, completed); } #endregion #region SendAsync Extensions public static void SendAsync(this HttpClient client, HttpMethod method, string uri, EventHandler completed) { SendAsync(client, method, uri, null, null, completed); } public static void SendAsync(this HttpClient client, HttpMethod method, string uri, object userState, EventHandler completed) { SendAsync(client, method, uri, null, null, userState, completed); } public static void SendAsync(this HttpClient client, HttpMethod method, Uri uri, EventHandler completed) { SendAsync(client, method, uri, null, null, completed); } public static void SendAsync(this HttpClient client, HttpMethod method, Uri uri, object userState, EventHandler completed) { SendAsync(client, method, uri, null, null, userState, completed); } public static void SendAsync(this HttpClient client, HttpMethod method, string uri, RequestHeaders headers, EventHandler completed) { SendAsync(client, method, uri, headers, null, completed); } public static void SendAsync(this HttpClient client, HttpMethod method, string uri, RequestHeaders headers, object userState, EventHandler completed) { SendAsync(client, method, uri, headers, null, userState, completed); } public static void SendAsync(this HttpClient client, HttpMethod method, string uri, HttpContent content, EventHandler completed) { SendAsync(client, method, uri, null, content, completed); } public static void SendAsync(this HttpClient client, HttpMethod method, string uri, HttpContent content, object userState, EventHandler completed) { SendAsync(client, method, uri, null, content, userState, completed); } public static void SendAsync(this HttpClient client, HttpMethod method, Uri uri, RequestHeaders headers, EventHandler completed) { SendAsync(client, method, uri, headers, null, completed); } public static void SendAsync(this HttpClient client, HttpMethod method, Uri uri, RequestHeaders headers, object userState, EventHandler completed) { SendAsync(client, method, uri, headers, null, userState, completed); } public static void SendAsync(this HttpClient client, HttpMethod method, Uri uri, HttpContent content, EventHandler completed) { SendAsync(client, method, uri, null, content, completed); } public static void SendAsync(this HttpClient client, HttpMethod method, Uri uri, HttpContent content, object userState, EventHandler completed) { SendAsync(client, method, uri, null, content, userState, completed); } public static void SendAsync(this HttpClient client, HttpMethod method, string uri, RequestHeaders headers, HttpContent content, EventHandler completed) { SendAsync(client, method, new Uri(uri, UriKind.RelativeOrAbsolute), headers, content, completed); } public static void SendAsync(this HttpClient client, HttpMethod method, string uri, RequestHeaders headers, HttpContent content, object userState, EventHandler completed) { SendAsync(client, method, new Uri(uri, UriKind.RelativeOrAbsolute), headers, content, userState, completed); } public static void SendAsync(this HttpClient client, HttpMethod method, Uri uri, RequestHeaders headers, HttpContent content, EventHandler completed) { SendAsync(client, new HttpRequestMessage(method.ToString(), uri, headers, content), completed); } public static void SendAsync(this HttpClient client, HttpMethod method, Uri uri, RequestHeaders headers, HttpContent content, object userState, EventHandler completed) { SendAsync(client, new HttpRequestMessage(method.ToString(), uri, headers, content), userState, completed); } public static void SendAsync(this HttpClient client, HttpRequestMessage message, EventHandler completed) { SendAsync(client, message, null, completed); } public static void SendAsync(this HttpClient client, HttpRequestMessage message, object userState, EventHandler completed) { client.SendCompleted += completed; client.SendAsync(message, userState); } #endregion private static void CheckNull(T o, string name) where T : class { if (o == null) { throw new ArgumentNullException(name); } } } }