private async Task <HttpResponse> GetResponse(NetCore11HttpRequest request) { HttpResponse response = new HttpResponse { ResponseStatus = ResponseStatus.None }; try { NetCore11HttpResponse webResponse = await GetRawResponse(request); this.ExtractResponseData(response, webResponse); } catch (Exception ex) { this.ExtractErrorResponse(response, ex); } return(response); }
/// <summary> /// Performs a HTTP action to get the response to this request. /// </summary> /// <returns></returns> public async Task <NetCore11HttpResponse> GetResponseAsync() { // Since most people will use IRestResponse.GetContent(), we should read the entire response including entity-body. Task <HttpResponseMessage> sendRequestTask = this.RequestClient.SendAsync(this.RequestMessage, HttpCompletionOption.ResponseContentRead); HttpResponseMessage responseMessage = await sendRequestTask.ConfigureAwait(false); var response = new NetCore11HttpResponse(responseMessage); if (!responseMessage.IsSuccessStatusCode) { throw new WebException( string.Format("{0} {1}", (int)response.StatusCode, response.StatusDescription), null, WebExceptionStatus.ProtocolError, response); } return(response); }
private void ExtractResponseData(HttpResponse response, NetCore11HttpResponse webResponse) { using (webResponse) { response.ContentEncoding = webResponse.Headers[HttpResponseHeader.ContentEncoding]; response.Server = webResponse.Headers[HttpResponseHeader.Server]; response.ContentType = webResponse.ContentType; response.ContentLength = webResponse.ContentLength; Stream webResponseStream = webResponse.GetResponseStream(); if (string.Equals(webResponse.Headers[HttpResponseHeader.ContentEncoding], "gzip", StringComparison.OrdinalIgnoreCase)) { GZipStream gzStream = new GZipStream(webResponseStream, CompressionMode.Decompress); ProcessResponseStream(gzStream, response); } else { ProcessResponseStream(webResponseStream, response); } response.StatusCode = webResponse.StatusCode; response.StatusDescription = webResponse.StatusDescription; response.ResponseUri = webResponse.ResponseUri; response.ResponseStatus = ResponseStatus.Completed; if (webResponse.Cookies != null) { foreach (Cookie cookie in webResponse.Cookies) { response.Cookies.Add(new HttpCookie { Comment = cookie.Comment, CommentUri = cookie.CommentUri, Discard = cookie.Discard, Domain = cookie.Domain, Expired = cookie.Expired, Expires = cookie.Expires, HttpOnly = cookie.HttpOnly, Name = cookie.Name, Path = cookie.Path, Port = cookie.Port, Secure = cookie.Secure, TimeStamp = cookie.TimeStamp, Value = cookie.Value, Version = cookie.Version }); } } foreach (string headerName in webResponse.Headers.AllKeys) { string headerValue = webResponse.Headers[headerName]; response.Headers.Add(new HttpHeader { Name = headerName, Value = headerValue }); } webResponse.Dispose(); } }