/// <summary> /// This method does an Http GET to the provided url and calls the responseCallback delegate /// providing it with the response returned from the remote server. /// </summary> /// <param name="url">The url to make an Http GET to</param> /// <param name="timeout">The timeout.</param> /// <param name="responseCallback">The callback delegate that should be called when the response returns from the remote server</param> /// <param name="state">Any state information you need to pass along to be available in the callback method when it is called</param> /// <param name="contentType">The Content-Type of the Http request</param> public static void GetAsync(string url, TimeSpan timeout, Action<HttpWebRequestCallbackState> responseCallback, object state = null, string contentType = "application/x-www-form-urlencoded") { HttpWebRequest httpWebRequest; try { httpWebRequest = CreateHttpWebRequest(url, "GET", contentType); } catch (Exception ex) { Log.WarnFormat("Exception creating WebRequest for url: {0}", ex, url); responseCallback(new HttpWebRequestCallbackState(ex, state)); return; } var requestState = new HttpWebRequestAsyncState { HttpWebRequest = httpWebRequest, ResponseCallback = responseCallback, State = state }; #if DISABLE_ASYNC ProcessRequest(requestState); #else var result = httpWebRequest.BeginGetResponse(BeginGetResponseCallback, requestState); ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, TimeOutCallback, httpWebRequest, timeout, true); #endif }
/// <summary> /// Begins the get response callback. /// </summary> /// <param name="asyncResult">The async result.</param> private static void BeginGetResponseCallback(IAsyncResult asyncResult) { WebResponse webResponse = null; Stream responseStream = null; HttpWebRequestAsyncState asyncState = null; try { asyncState = (HttpWebRequestAsyncState)asyncResult.AsyncState; webResponse = asyncState.HttpWebRequest.EndGetResponse(asyncResult); responseStream = webResponse.GetResponseStream(); var webRequestCallbackState = new HttpWebRequestCallbackState((HttpWebResponse)webResponse, asyncState.State); asyncState.ResponseCallback(webRequestCallbackState); responseStream.Close(); responseStream = null; webResponse.Close(); webResponse = null; } catch (Exception ex) { if (asyncState != null) { Log.WarnFormat("Exception requesting url: {0}", ex, asyncState.HttpWebRequest.RequestUri); asyncState.ResponseCallback(new HttpWebRequestCallbackState(ex, asyncState.State)); } else { Log.Warn("BeginGetResponseCallback", ex); throw; } } finally { if (responseStream != null) { responseStream.Close(); } if (webResponse != null) { webResponse.Close(); } } }
static void ProcessRequest(HttpWebRequestAsyncState asyncState) { WebResponse webResponse = null; Stream responseStream = null; try { webResponse = asyncState.HttpWebRequest.GetResponse(); responseStream = webResponse.GetResponseStream(); var webRequestCallbackState = new HttpWebRequestCallbackState((HttpWebResponse)webResponse, asyncState.State); asyncState.ResponseCallback(webRequestCallbackState); if (responseStream != null) responseStream.Close(); responseStream = null; webResponse.Close(); webResponse = null; } catch (Exception ex) { if (asyncState != null) { Log.WarnFormat("Exception requesting url: {0}", ex, asyncState.HttpWebRequest.RequestUri); asyncState.ResponseCallback(new HttpWebRequestCallbackState(ex, asyncState.State)); } else { Log.Warn("BeginGetResponseCallback", ex); throw; } } finally { if (responseStream != null) responseStream.Close(); if (webResponse != null) webResponse.Close(); } }