示例#1
0
        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);
            }
        }
示例#2
0
        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);
        }
示例#3
0
        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);
                }
            }
        }
示例#4
0
        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);
        }