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; }
public RestResponse ExtractResponse(HttpWebResponse webresponse) { var restresponse = new RestResponse(); restresponse.StatusCode = webresponse.StatusCode; restresponse.StatusDescription = webresponse.StatusDescription; var stream = webresponse.GetResponseStream(); restresponse.RawBytes = stream.ReadAsBytes(); restresponse.ResponseStatus = ResponseStatus.Completed; return restresponse; }
public void When_Response_With_Invalid_Json_Needs_Deserialization_Then_Deserialize_To_Type() { var sourcecontent = "{ \"firstName\":'', \"lastName\":\"Doe\"}"; var sourcebytes = Encoding.UTF8.GetBytes(sourcecontent); var client = new RestClient(); var restrequest = new RestRequest(); var restresponse = new RestResponse(); restresponse.ResponseStatus = ResponseStatus.Completed; restresponse.RawBytes = sourcebytes; var actualuser = client.Deserialize<User>(restrequest, restresponse); Assert.IsNotNull(actualuser.ErrorException); }
private void DeserializeResponse <T>(RestRequest request, Action <RestResponse <T>, RestRequestAsyncHandle> callback, RestResponse response, RestRequestAsyncHandle asyncHandle) { callback(Deserialize <T>(request, response), asyncHandle); }
public async Task <RestResponse> ExecuteAsync(RestRequest restrequest, CancellationToken cancellationToken) { var handler = new HttpClientHandler(); if (this.Proxy != null) { handler.Proxy = this.Proxy; } //HttpClient client = new HttpClient(handler); //client.Timeout = new TimeSpan(0, 0, this.Timeout); //if (!string.IsNullOrWhiteSpace(this.UserAgent)) //{ // client.DefaultRequestHeaders.Add("User-Agent", this.UserAgent); //} //client.DefaultRequestHeaders.Add("Accept", "application/json"); //client.DefaultRequestHeaders.Add("Accept-Charset", "utf-8"); this.Instance.Timeout = new TimeSpan(0, 0, this.Timeout); if (!string.IsNullOrWhiteSpace(this.UserAgent)) { this.Instance.DefaultRequestHeaders.Add("User-Agent", this.UserAgent); } this.Instance.DefaultRequestHeaders.Add("Accept", "application/json"); this.Instance.DefaultRequestHeaders.Add("Accept-Charset", "utf-8"); var request = ConfigureRequestMessage(restrequest); var restresponse = new RestResponse() { ResponseStatus = ResponseStatus.None }; try { var response = await this.Instance.SendAsync(request, cancellationToken); //var response = await client.SendAsync(request, cancellationToken); restresponse.StatusCode = response.StatusCode; restresponse.StatusDescription = response.ReasonPhrase; if (response.Content != null) { restresponse.RawBytes = await response.Content.ReadAsByteArrayAsync(); } restresponse.ResponseStatus = ResponseStatus.Completed; } catch (TaskCanceledException exc) { restresponse.ErrorException = exc; restresponse.ErrorMessage = exc.Message; restresponse.ResponseStatus = ResponseStatus.Error; } catch (Exception exc) { restresponse.ErrorException = exc; restresponse.ErrorMessage = exc.Message; restresponse.ResponseStatus = ResponseStatus.Error; Debug.WriteLine(exc.Message); } return(restresponse); }