private void Send(RestState state) { #if SILVERLIGHT var webRequest = (HttpWebRequest)System.Net.Browser.WebRequestCreator.ClientHttp.Create(state.EndPoint); if (!state.IsGet()) { webRequest.ContentType = RequestContentType; } #else var webRequest = (HttpWebRequest)WebRequest.Create(state.EndPoint); webRequest.AllowAutoRedirect = true; webRequest.ContentType = RequestContentType; webRequest.Accept = ResponseContentType; webRequest.UserAgent = "Exceptionless/" + ThisAssembly.AssemblyFileVersion; webRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None; #endif webRequest.Method = state.Method; if (!state.IsGet()) { webRequest.Headers["Content-Encoding"] = "gzip"; } if (AuthorizationHeader != null) { webRequest.Headers[HttpRequestHeader.Authorization] = AuthorizationHeader.ToString(); } if (!state.IsGet() && !state.IsPost() && UseMethodOverride) { webRequest.Method = "POST"; webRequest.Headers[HTTP_METHOD_OVERRIDE] = state.Method; } CopyHeaders(RequestHeaders, webRequest.Headers); try { webRequest.UseDefaultCredentials = true; if (Credentials != null) { webRequest.Credentials = Credentials; } } catch (Exception) {} #if !SILVERLIGHT if (Proxy != null) { webRequest.Proxy = Proxy; } #endif state.Request = webRequest; if (state.IsGet()) { BeginGetResponse(state); } else { webRequest.BeginGetRequestStream(OnRequestStream, state); } }
public void SendAsync <TRequest, TResponse>(Uri endPoint, TRequest data, string method, object userToken) { RestState state = CreateState <TRequest, TResponse>(endPoint, data, method); state.UserToken = userToken; Send(state); }
private void BeginGetResponse(RestState state) { IAsyncResult handle = state.Request.BeginGetResponse(OnResponse, state); #if !SILVERLIGHT // create async timeout ThreadPool.RegisterWaitForSingleObject( handle.AsyncWaitHandle, OnTimeout, state, Timeout, true); #endif }
private RestState CreateState <TRequest, TResponse>(Uri endPoint, TRequest data, string method) { Reset(); var state = new RestState(); state.EndPoint = new Uri(BaseUri, endPoint); state.RequestData = data; state.RequestType = typeof(TRequest); state.ResponseType = typeof(TResponse); state.Method = method; return(state); }
private object ReadResponseType(Stream stream, RestState state) { if (stream == null || state.Response == null) { return(null); } string contentType = new ContentTypeHeader(state.Response.ContentType).ContentType ?? DEFAULT_CONTENT_TYPE; if (!contentType.Equals(DEFAULT_CONTENT_TYPE)) { return(null); } return(ModelSerializer.Current.Deserialize(stream, state.ResponseType)); }
protected void OnRestRequestComplete(RestState state) { EventHandler <RestRequestCompletedEventArgs> handle = RestRequestCompleted; if (handle == null) { return; } var e = new RestRequestCompletedEventArgs( state.Error, state.IsCancelled, state.UserToken, state); handle.Invoke(this, e); }
private void CompleteProcess(RestState state) { try { HttpWebResponse webResponse = state.Response; if (webResponse != null) { #if SILVERLIGHT if (webResponse.SupportsHeaders) { CopyHeaders(webResponse.Headers, ResponseHeaders); } #else CopyHeaders(webResponse.Headers, ResponseHeaders); #endif StatusCode = (int)webResponse.StatusCode; Status = String.Format("{0} {1}", StatusCode, webResponse.StatusDescription); } if (state.Error == null) { return; } Error = state.Error; var webEx = Error as WebException; if (webEx != null && String.IsNullOrEmpty(Status)) { Status = webEx.Status.ToString().ToSpacedWords(); } } catch (Exception ex) { Error = ex; } finally { OnRestRequestComplete(state); _complete.Set(); } }
private string ReadResponseString(Stream stream, RestState state) { byte[] data = ReadResponseBytes(stream); return(GetStringUsingEncoding(state.Request, data)); }
public TResponse Send <TRequest, TResponse>(Uri endPoint, TRequest data, string method) where TResponse : class { #if SILVERLIGHT if (System.Windows.Deployment.Current.Dispatcher.CheckAccess()) { throw new InvalidOperationException("Invoking this method on the UI thread is forbidden."); } #endif RestState state = null; // loop for trying authentication for (int i = 0; i < 2; i++) { state = CreateState <TRequest, TResponse>(endPoint, data, method); Send(state); // wait for async requests _complete.WaitOne(); // if request was unauthorized if (StatusCode != 401 || AuthorizationCallback == null) { break; } // try to authenticate var authorizationState = new AuthorizationState(); authorizationState.IsRefresh = true; AuthorizationCallback(authorizationState); AuthorizationHeader = authorizationState.Header; if (authorizationState.IsAuthenticated) { continue; } if (authorizationState.Error != null) { Error = authorizationState.Error; } break; } if (state == null) { return(default(TResponse)); } if (typeof(HttpWebResponse) == typeof(TResponse)) { return(state.Response as TResponse); } if (!state.Response.IsSuccessStatusCode()) { return(default(TResponse)); } return(state.ResponseData == null ? default(TResponse) : (TResponse)state.ResponseData); }
public RestRequestCompletedEventArgs(Exception error, bool cancelled, object userState, RestState restState) : base(error, cancelled, userState) { RestState = restState; }