private static IEnumerable <Task> RequestAsyncTask(string url, object userState, TaskCompletionSource <WebServiceResponse> tcs) { Logs.Message("WebService -> Creating web request " + url); var response = new WebServiceResponse(); response.RequestUri = url; response.UserState = userState; var httpRequest = (HttpWebRequest)WebRequest.Create(url); Logs.Message("WebService -> Getting web response..."); var httpTask = Task.Factory.FromAsync <WebResponse> (httpRequest.BeginGetResponse, httpRequest.EndGetResponse, null); yield return(httpTask); try { // get web response using (var httpResponse = (HttpWebResponse)httpTask.Result) { response = Update(response, httpResponse); tcs.TrySetResult(response); } } catch (Exception error) { response = Update(response, error); tcs.TrySetResult(response); } }
private static WebServiceResponse Update(WebServiceResponse response, Exception ex) { Logs.Message("WebService -> Getting web response failed: " + ex.Message); response.StatusCode = HttpStatusCode.NotAcceptable; response.ErrorMessage = ex.Message; response.ErrorStack = ex.Message + "\n" + ex.StackTrace; response.ErrorOccured = true; return(response); }
public static void Request(string url, object userState = null, Action <WebServiceResponse> callback = null) { Logs.Message("WebService -> Creating web request " + url + "..."); try { var httpRequest = (HttpWebRequest)WebRequest.Create(url); Logs.Message("WebService -> Getting web response..."); var result = (IAsyncResult)httpRequest.BeginGetResponse( new AsyncCallback(delegate(IAsyncResult tempResult) { try { var httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(tempResult); var response = new WebServiceResponse(); response.RequestUri = url; response.UserState = userState; response = Update(response, httpResponse); if (callback != null) { callback(response); } } catch (WebException e) { var response = new WebServiceResponse(); response.RequestUri = url; response.UserState = userState; response = Update(response, e); if (callback != null) { callback(response); } } }), null); } catch (Exception error) { var response = new WebServiceResponse(); response.RequestUri = url; response.UserState = userState; response = Update(response, error); if (callback != null) { callback(response); } } }
private static WebServiceResponse Update( WebServiceResponse response, HttpWebResponse httpResponse) { Logs.Message("WebService -> Getting web response completed: " + httpResponse.StatusDescription); response.Uri = httpResponse.ResponseUri; response.Method = httpResponse.Method; response.StatusCode = httpResponse.StatusCode; response.StatusInfo = httpResponse.StatusDescription; response.ResultType = httpResponse.ContentType; response.ResultLength = httpResponse.ContentLength; Logs.Message("WebService -> Getting web response stream"); response.ResultStream = httpResponse.GetResponseStream(); return(response); }