예제 #1
0
 private void SetTimeout(IAsyncResult asyncResult, TimeOutState timeOutState)
 {
     if (Timeout != 0)
     {
         ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, TimeoutCallback, timeOutState, Timeout, executeOnlyOnce: true);
     }
 }
예제 #2
0
        private HttpWebRequest GetStyleMethodInternalAsync(string method, Action <HttpResponse> callback)
        {
            HttpWebRequest webRequest = null;

            try
            {
                var url = Url;

                webRequest = ConfigureAsyncWebRequest(method, url);

                if (HasBody && (method == "DELETE" || method == "OPTIONS"))
                {
                    webRequest.ContentType = RequestContentType;
                    WriteRequestBodyAsync(webRequest, callback);
                }
                else
                {
                    this.timeoutState = new TimeOutState {
                        Request = webRequest
                    };

                    var asyncResult = webRequest.BeginGetResponse(result => ResponseCallback(result, callback), webRequest);

                    SetTimeout(asyncResult, this.timeoutState);
                }
            }
            catch (Exception ex)
            {
                ExecuteCallback(CreateErrorResponse(ex), callback);
            }

            return(webRequest);
        }
        public RestResponse ParseWebException(WebException exc, TimeOutState timeoutstate) 
        {
            var restresponse = new RestResponse();

            // Check to see if this is an HTTP error or a transport error.
            // In cases where an HTTP error occurs ( status code >= 400 )
            // return the underlying HTTP response, otherwise assume a
            // transport exception (ex: connection timeout) and
            // rethrow the exception
            if (exc.Response is HttpWebResponse)
            {
                var errorresponse = exc.Response as HttpWebResponse;
                restresponse = ExtractResponse(errorresponse);
            }
            else
            {
                restresponse.ErrorException = exc;
                restresponse.ErrorMessage = exc.Message;

                if (timeoutstate != null && exc.Status == WebExceptionStatus.RequestCanceled)
                {
                    restresponse.ResponseStatus = timeoutstate.TimedOut ? ResponseStatus.TimedOut : ResponseStatus.Aborted;
                }
                else
                {
                    restresponse.ResponseStatus = ResponseStatus.Error;
                }
            }

            return restresponse;
        }
예제 #4
0
        private void WriteRequestBodyAsync(HttpWebRequest webRequest, Action <IHttpResponse> callback)
        {
            _timeoutState = new TimeOutState
            {
                Request = webRequest
            };
            IAsyncResult asyncResult;

            if (HasBody || HasFiles || AlwaysMultipartFormData)
            {
                webRequest.ContentLength = CalculateContentLength();
                asyncResult = webRequest.BeginGetRequestStream(delegate(IAsyncResult result)
                {
                    RequestStreamCallback(result, callback);
                }, webRequest);
            }
            else
            {
                asyncResult = webRequest.BeginGetResponse(delegate(IAsyncResult r)
                {
                    ResponseCallback(r, callback);
                }, webRequest);
            }
            SetTimeout(asyncResult, _timeoutState);
        }
예제 #5
0
 private void SetTimeout(IAsyncResult asyncResult, TimeOutState timeOutState)
 {
     if (Timeout != 0)
     {
         ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), timeOutState, Timeout, true);
     }
 }
예제 #6
0
        private HttpWebRequest GetStyleMethodInternalAsync(string method, Action <IHttpResponse> callback)
        {
            HttpWebRequest httpWebRequest = null;

            try
            {
                httpWebRequest = ConfigureWebRequest(method, Url);
                if (HasBody && (method == "DELETE" || method == "OPTIONS"))
                {
                    httpWebRequest.ContentType = RequestContentType;
                    WriteRequestBodyAsync(httpWebRequest, callback);
                }
                else
                {
                    _timeoutState = new TimeOutState
                    {
                        Request = httpWebRequest
                    };
                    LogHttpRequest(httpWebRequest);
                    IAsyncResult asyncResult = httpWebRequest.BeginGetResponse(delegate(IAsyncResult result)
                    {
                        ResponseCallback(result, callback);
                    }, httpWebRequest);
                    SetTimeout(asyncResult, _timeoutState);
                }
            }
            catch (Exception ex)
            {
                ExecuteCallback(CreateErrorResponse(ex), callback);
            }
            return(httpWebRequest);
        }
예제 #7
0
		private void SetTimeout(IAsyncResult asyncResult, HttpWebRequest request, TimeOutState timeOutState)
		{
#if FRAMEWORK
			if (Timeout != 0)
			{
				ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), timeOutState, Timeout, true);
			} 
#endif		
		}
예제 #8
0
        private void SetTimeout(IAsyncResult asyncResult, TimeOutState timeOutState)
        {
#if FRAMEWORK && !PocketPC
            if (Timeout != 0)
            {
                ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle,
                                                       TimeoutCallback, timeOutState, Timeout, true);
            }
#endif
        }
예제 #9
0
        private void SetTimeout(IAsyncResult asyncResult, TimeOutState timeOutState)
        {
#if UNITY_5_0_OR_NEWER || UNITY_2017_1_OR_NEWER
            if (this.Timeout != 0)
            {
                ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle,
                                                       TimeoutCallback, timeOutState, this.Timeout, true);
            }
#endif
        }
예제 #10
0
        void WriteRequestBodyAsync(HttpWebRequest webRequest, Action<HttpResponse> callback)
        {
            IAsyncResult asyncResult;
            _timeoutState = new TimeOutState {Request = webRequest};

            if (HasBody || HasFiles || AlwaysMultipartFormData)
            {
                webRequest.ContentLength = CalculateContentLength();

                asyncResult = webRequest.BeginGetRequestStream(
                    RequestStreamCallback, webRequest
                );
            }
            else
            {
                asyncResult = webRequest.BeginGetResponse(r => ResponseCallback(r, callback), webRequest);
            }

            SetTimeout(asyncResult);

            void RequestStreamCallback(IAsyncResult result)
            {
                if (_timeoutState.TimedOut)
                {
                    var response = new HttpResponse {ResponseStatus = ResponseStatus.TimedOut};

                    ExecuteCallback(response, callback);

                    return;
                }

                // write body to request stream
                try
                {
                    using (var requestStream = webRequest.EndGetRequestStream(result))
                    {
                        if (HasFiles || AlwaysMultipartFormData)
                            WriteMultipartFormData(requestStream);
                        else if (RequestBodyBytes != null)
                            requestStream.Write(RequestBodyBytes, 0, RequestBodyBytes.Length);
                        else if (RequestBody != null)
                            requestStream.WriteString(RequestBody, Encoding);
                    }

                    var response = webRequest.BeginGetResponse(r => ResponseCallback(r, callback), webRequest);

                    SetTimeout(response);
                }
                catch (Exception ex)
                {
                    ExecuteCallback(CreateErrorResponse(ex), callback);
                }
            }
        }
예제 #11
0
 private static void TimeoutCallback(object state, bool timedOut)
 {
     if (timedOut)
     {
         TimeOutState timeOutState = state as TimeOutState;
         if (timeOutState != null)
         {
             lock (timeOutState)
             {
                 timeOutState.TimedOut = true;
             }
             if (timeOutState.Request != null)
             {
                 timeOutState.Request.Abort();
             }
         }
     }
 }
예제 #12
0
		private void GetStyleMethodInternalAsync(string method, Action<HttpResponse> callback)
		{
			try
			{
				var url = Url;
				var webRequest = ConfigureAsyncWebRequest(method, url);
				timeoutState = new TimeOutState { Request = webRequest };
				var asyncResult = webRequest.BeginGetResponse(result => ResponseCallback(result, callback), webRequest);
				SetTimeout(asyncResult, webRequest, timeoutState);
			}
			catch (Exception ex)
			{
				var response = new HttpResponse();
				response.ErrorMessage = ex.Message;
				response.ErrorException = ex;
				response.ResponseStatus = ResponseStatus.Error;
				ExecuteCallback(response, callback);
			}
		}
예제 #13
0
		private void GetStyleMethodInternalAsync(string method, Action<HttpResponse> callback)
		{
			try
			{
				var url = Url;
				var webRequest = ConfigureAsyncWebRequest(method, url);
				timeoutState = new TimeOutState { Request = webRequest };
				var asyncResult = webRequest.BeginGetResponse(result => ResponseCallback(result, callback), webRequest);
				SetTimeout(asyncResult, webRequest, timeoutState);
			}
			catch (Exception ex)
			{
				var response = new HttpResponse();
				response.ErrorMessage = ex.Message;
				response.ErrorException = ex;
				response.ResponseStatus = ResponseStatus.Error;
				ExecuteCallback(response, callback);
			}
		}
예제 #14
0
        private HttpWebRequest GetStyleMethodInternalAsync(string method, Action <HttpResponse> callback)
        {
            HttpWebRequest webRequest = null;

            try
            {
                var url = Url;
                webRequest    = ConfigureAsyncWebRequest(method, url);
                _timeoutState = new TimeOutState {
                    Request = webRequest
                };
                var asyncResult = webRequest.BeginGetResponse(result => ResponseCallback(result, callback), webRequest);
                SetTimeout(asyncResult, _timeoutState);
            }
            catch (Exception ex)
            {
                ExecuteCallback(CreateErrorResponse(ex), callback);
            }
            return(webRequest);
        }
예제 #15
0
        private void WriteRequestBodyAsync(HttpWebRequest webRequest, Action <HttpResponse> callback)
        {
            IAsyncResult asyncResult;

            _timeoutState = new TimeOutState {
                Request = webRequest
            };

            if (HasBody || HasFiles)
            {
                asyncResult = webRequest.BeginGetRequestStream(result => RequestStreamCallback(result, callback), webRequest);
            }

            else
            {
                asyncResult = webRequest.BeginGetResponse(r => ResponseCallback(r, callback), webRequest);
            }

            SetTimeout(asyncResult, _timeoutState);
        }
예제 #16
0
		private void WriteRequestBodyAsync(HttpWebRequest webRequest, Action<HttpResponse> callback)
		{
			IAsyncResult asyncResult;
			timeoutState = new TimeOutState { Request = webRequest };

			if (HasBody || HasFiles)
			{
#if !WINDOWS_PHONE
				webRequest.ContentLength = CalculateContentLength();
#endif
				asyncResult = webRequest.BeginGetRequestStream(result => RequestStreamCallback(result, callback), webRequest);
			}

			else
			{
				asyncResult = webRequest.BeginGetResponse(r => ResponseCallback(r, callback), webRequest);
			}

			SetTimeout(asyncResult, webRequest, timeoutState);
		}
예제 #17
0
		private void TimeoutCallback(object state, bool timedOut)
		{
			if (timedOut)
			{
				TimeOutState timeoutState = state as TimeOutState;

				if (timeoutState == null)
				{
					return;
				}

				lock (timeoutState)
				{
					timeoutState.TimedOut = timedOut;
				}

				if (timeoutState.Request != null)
				{
					timeoutState.Request.Abort();
				}
			}
		}
예제 #18
0
        private void WriteRequestBodyAsync(HttpWebRequest webRequest, Action <HttpResponse> callback)
        {
            IAsyncResult asyncResult;

            _timeoutState = new TimeOutState {
                Request = webRequest
            };

            if (HasBody || HasFiles || AlwaysMultipartFormData)
            {
#if !WINDOWS_PHONE && !PocketPC && !NETFX_CORE
                webRequest.ContentLength = CalculateContentLength();
#endif
                asyncResult = webRequest.BeginGetRequestStream(result => RequestStreamCallback(result, callback), webRequest);
            }

            else
            {
                asyncResult = webRequest.BeginGetResponse(r => ResponseCallback(r, callback), webRequest);
            }

            SetTimeout(asyncResult, _timeoutState);
        }
예제 #19
0
        private void WriteRequestBodyAsync(HttpWebRequest webRequest, Action <HttpResponse> callback)
        {
            IAsyncResult asyncResult;

            this.timeoutState = new TimeOutState {
                Request = webRequest
            };

            if (this.HasBody || this.HasFiles || this.AlwaysMultipartFormData)
            {
#if !WINDOWS_PHONE && !WINDOWS_UWP && !(NETCORE50 || NETSTANDARD1_5 || NETSTANDARD2_0)
                webRequest.ContentLength = this.CalculateContentLength();
#endif
                asyncResult = webRequest.BeginGetRequestStream(
                    result => this.RequestStreamCallback(result, callback), webRequest);
            }
            else
            {
                asyncResult = webRequest.BeginGetResponse(r => this.ResponseCallback(r, callback), webRequest);
            }

            this.SetTimeout(asyncResult, this.timeoutState);
        }
예제 #20
0
		private void SetTimeout(IAsyncResult asyncResult, TimeOutState timeOutState)
		{
			if (Timeout != 0)
			{
				ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), timeOutState, Timeout, true);
			} 
		}
예제 #21
0
        private void WriteRequestBodyAsync(HttpWebRequest webRequest, Action<HttpResponse> callback)
        {
            IAsyncResult asyncResult;
            _timeoutState = new TimeOutState { Request = webRequest };

            if (HasBody || HasFiles)
            {
                asyncResult = webRequest.BeginGetRequestStream(result => RequestStreamCallback(result, callback), webRequest);
            }

            else
            {
                asyncResult = webRequest.BeginGetResponse(r => ResponseCallback(r, callback), webRequest);
            }

            SetTimeout(asyncResult, _timeoutState);
        }
예제 #22
0
        private void SetTimeout(IAsyncResult asyncResult, TimeOutState timeOutState)
        {
#if FRAMEWORK && !PocketPC
            if (Timeout != 0)
            {
                ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle,
                    TimeoutCallback, timeOutState, Timeout, true);
            }
#endif
        }
예제 #23
0
		private void SetTimeout(IAsyncResult asyncResult, HttpWebRequest request, TimeOutState timeOutState)
		{
#if FRAMEWORK
			if (Timeout != 0)
			{
				ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), timeOutState, Timeout, true);
			} 
#endif		
		}
예제 #24
0
		private void WriteRequestBodyAsync(HttpWebRequest webRequest, Action<HttpResponse> callback)
		{
			IAsyncResult asyncResult;
			timeoutState = new TimeOutState { Request = webRequest };

			if (HasBody || HasFiles || HasBytes)
			{
#if !WINDOWS_PHONE
				webRequest.ContentLength = CalculateContentLength();
#endif
				asyncResult = webRequest.BeginGetRequestStream(result => RequestStreamCallback(result, callback), webRequest);
			}

			else
			{
				asyncResult = webRequest.BeginGetResponse(r => ResponseCallback(r, callback), webRequest);
			}

			SetTimeout(asyncResult, webRequest, timeoutState);
		}
예제 #25
0
 private HttpWebRequest GetStyleMethodInternalAsync(string method, Action<HttpResponse> callback)
 {
     HttpWebRequest webRequest = null;
     try
     {
         var url = Url;
         webRequest = ConfigureAsyncWebRequest(method, url);
         _timeoutState = new TimeOutState { Request = webRequest };
         var asyncResult = webRequest.BeginGetResponse(result => ResponseCallback(result, callback), webRequest);
         SetTimeout(asyncResult, _timeoutState);
     }
     catch(Exception ex)
     {
         ExecuteCallback(CreateErrorResponse(ex), callback);
     }
     return webRequest;
 }
예제 #26
0
		private void WriteRequestBodyAsync(HttpWebRequest webRequest, Action<HttpResponse> callback)
		{
			IAsyncResult asyncResult;
			_timeoutState = new TimeOutState { Request = webRequest };

			if (HasBody || HasFiles || AlwaysMultipartFormData)
			{
				webRequest.ContentLength = CalculateContentLength();
				asyncResult = webRequest.BeginGetRequestStream(result => RequestStreamCallback(result, callback), webRequest);
			}

			else
			{
				asyncResult = webRequest.BeginGetResponse(r => ResponseCallback(r, callback), webRequest);
			}

			SetTimeout(asyncResult, _timeoutState);
		}
예제 #27
0
        private void WriteRequestBodyAsync(HttpWebRequest webRequest, Action<HttpResponse> callback)
        {
            IAsyncResult asyncResult;
            this.timeoutState = new TimeOutState { Request = webRequest };

            if (this.HasBody || this.HasFiles || this.AlwaysMultipartFormData)
            {
#if !WINDOWS_PHONE
                webRequest.ContentLength = this.CalculateContentLength();
#endif
                asyncResult = webRequest.BeginGetRequestStream(
                    result => this.RequestStreamCallback(result, callback), webRequest);
            }
            else
            {
                asyncResult = webRequest.BeginGetResponse(r => this.ResponseCallback(r, callback), webRequest);
            }

            this.SetTimeout(asyncResult, this.timeoutState);
        }
예제 #28
0
	    private HttpWebRequest GetStyleMethodInternalAsync(string method, Action<HttpResponse> callback)
		{
			HttpWebRequest webRequest = null;
			try
			{
				var url = Url;
				webRequest = ConfigureAsyncWebRequest(method, url);
                if (HasBody && (method == "DELETE" || method == "OPTIONS"))
                {
                    webRequest.ContentType = RequestContentType;
                    WriteRequestBodyAsync(webRequest, callback);
                }
                else
                {
                    _timeoutState = new TimeOutState { Request = webRequest };
                    var asyncResult = webRequest.BeginGetResponse(result => ResponseCallback(result, callback), webRequest);
                    SetTimeout(asyncResult, _timeoutState);
                }
			}
			catch(Exception ex)
			{
				ExecuteCallback(CreateErrorResponse(ex), callback);
			}
			return webRequest;
		}
예제 #29
0
 private HttpWebRequest GetStyleMethodInternalAsync(string method, Action<HttpResponse> callback, object userState)
 {
     HttpWebRequest webRequest = null;
     try
     {
         var url = Url;
         webRequest = ConfigureAsyncWebRequest(method, url);
         _timeoutState = new TimeOutState { Request = webRequest };
         var asyncResult = webRequest.BeginGetResponse(result => ResponseCallback(result, callback, userState), webRequest);
         SetTimeout(asyncResult, _timeoutState);
     }
     catch(Exception ex)
     {
         var response = new HttpResponse();
         response.ErrorMessage = ex.Message;
         response.ErrorException = ex;
         response.ResponseStatus = ResponseStatus.Error;
         response.UserState = userState;
         ExecuteCallback(response, callback);
     }
     return webRequest;
 }