private void SetTimeout(IAsyncResult asyncResult, TimeOutState timeOutState) { if (Timeout != 0) { ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, TimeoutCallback, timeOutState, Timeout, executeOnlyOnce: true); } }
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; }
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); }
private void SetTimeout(IAsyncResult asyncResult, TimeOutState timeOutState) { if (Timeout != 0) { ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), timeOutState, Timeout, true); } }
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); }
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 }
private void SetTimeout(IAsyncResult asyncResult, TimeOutState timeOutState) { #if FRAMEWORK && !PocketPC if (Timeout != 0) { ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, TimeoutCallback, timeOutState, Timeout, true); } #endif }
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 }
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); } } }
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(); } } } }
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); } }
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); }
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); }
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); }
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(); } } }
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); }
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); }
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); }
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); }
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; }
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); }
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); }
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; }
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; }