HTTP response data
상속: IHttpResponse
예제 #1
0
파일: Http.cs 프로젝트: beilharz/RestSharp
        private static void ExtractResponseData(HttpResponse response, HttpWebResponse webResponse)
        {
            using (webResponse)
            {
            #if FRAMEWORK
                response.ContentEncoding = webResponse.ContentEncoding;
                response.Server = webResponse.Server;
            #endif
                response.ContentType = webResponse.ContentType;
                response.ContentLength = webResponse.ContentLength;
            #if WINDOWS_PHONE
                if (string.Equals(webResponse.Headers[HttpRequestHeader.ContentEncoding], "gzip", StringComparison.OrdinalIgnoreCase))
                    response.RawBytes = new GZipStream(webResponse.GetResponseStream()).ReadAsBytes();
                else
                    response.RawBytes = webResponse.GetResponseStream().ReadAsBytes();
            #else
                response.RawBytes = webResponse.GetResponseStream().ReadAsBytes();
            #endif
                //response.Content = GetString(response.RawBytes);
                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 (var headerName in webResponse.Headers.AllKeys)
                {
                    var headerValue = webResponse.Headers[headerName];
                    response.Headers.Add(new HttpHeader { Name = headerName, Value = headerValue });
                }

                webResponse.Close();
            }
        }
예제 #2
0
		private void RequestStreamCallback(IAsyncResult result, Action<HttpResponse> callback)
		{
			var webRequest = (HttpWebRequest)result.AsyncState;

			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
					{
						WriteStringTo(requestStream, RequestBody);
					}
				}
			}
			catch (Exception ex)
			{
				ExecuteCallback(CreateErrorResponse(ex), callback);
				return;
			}

			webRequest.BeginGetResponse(r => ResponseCallback(r, callback), webRequest);
		}
예제 #3
0
		private RestResponse ConvertToRestResponse(IRestRequest request, HttpResponse httpResponse)
		{
			var restResponse = new RestResponse();
			restResponse.Content = httpResponse.Content;
			restResponse.ContentEncoding = httpResponse.ContentEncoding;
			restResponse.ContentLength = httpResponse.ContentLength;
			restResponse.ContentType = httpResponse.ContentType;
			restResponse.ErrorException = httpResponse.ErrorException;
			restResponse.ErrorMessage = httpResponse.ErrorMessage;
			restResponse.RawBytes = httpResponse.RawBytes;
			restResponse.ResponseStatus = httpResponse.ResponseStatus;
			restResponse.ResponseUri = httpResponse.ResponseUri;
			restResponse.Server = httpResponse.Server;
			restResponse.StatusCode = httpResponse.StatusCode;
			restResponse.StatusDescription = httpResponse.StatusDescription;
			restResponse.Request = request;

			foreach (var header in httpResponse.Headers)
			{
				restResponse.Headers.Add(new Parameter { Name = header.Name, Value = header.Value, Type = ParameterType.HttpHeader });
			}

			foreach (var cookie in httpResponse.Cookies)
			{
				restResponse.Cookies.Add(new RestResponseCookie {
					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
				});
			}

			return restResponse;
		}
예제 #4
0
        private void ExtractResponseData(HttpResponse response, HttpWebResponse webResponse)
        {
            using (webResponse)
            {
#if FRAMEWORK
                response.ContentEncoding = webResponse.ContentEncoding;
                response.Server = webResponse.Server;
#endif
                response.ContentType = webResponse.ContentType;
                response.ContentLength = webResponse.ContentLength;

                Stream webResponseStream = webResponse.GetResponseStream();

#if WINDOWS_PHONE || UNITY
                string contentEncoding = "";
                if(webResponse.Headers[HttpResponseHeader.ContentEncoding] != null) { 
                    contentEncoding = webResponse.Headers[HttpResponseHeader.ContentEncoding];
                }
                    
                if (string.Equals(contentEncoding, "gzip", StringComparison.OrdinalIgnoreCase))
                {
                    GZipStream gzStream = new GZipStream(webResponseStream);

                    ProcessResponseStream(gzStream, response);
                }
                else if (string.Equals(contentEncoding, "deflate", StringComparison.OrdinalIgnoreCase))
                {
                    ZlibStream dfStream = new ZlibStream(webResponseStream);
                    
                    ProcessResponseStream(dfStream, response);
                }
                else
                { 
                    ProcessResponseStream(webResponseStream, response);
                }
#else
                this.ProcessResponseStream(webResponseStream, response);
#endif

                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.Close();
            }
        }
예제 #5
0
		private void ExtractResponseData(HttpResponse response, HttpWebResponse webResponse)
		{
			using (webResponse)
			{
				response.ContentType = webResponse.ContentType;
				response.ContentLength = webResponse.ContentLength;
				Stream webResponseStream = webResponse.GetResponseStream();

				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 (var headerName in webResponse.Headers.AllKeys)
				{
					var headerValue = webResponse.Headers[headerName];
					response.Headers.Add(new HttpHeader { Name = headerName, Value = headerValue });
				}

				webResponse.Close();
			}
		}
예제 #6
0
        private void ExtractErrorResponse(HttpResponse httpResponse, Exception ex)
        {
            WebException webException = ex as WebException;

            if (webException != null && webException.Status == WebExceptionStatus.Timeout)
            {
                httpResponse.ResponseStatus = ResponseStatus.TimedOut;
                httpResponse.ErrorMessage = ex.Message;
                httpResponse.ErrorException = webException;

                return;
            }

            httpResponse.ErrorMessage = ex.Message;
            httpResponse.ErrorException = ex;
            httpResponse.ResponseStatus = ResponseStatus.Error;
        }
예제 #7
0
 private void ExecuteCallback(HttpResponse response, Action<HttpResponse> callback)
 {
     #if WINDOWS_PHONE
     var dispatcher = Deployment.Current.Dispatcher;
     dispatcher.BeginInvoke(() =>
     {
     #endif
     callback(response);
     #if WINDOWS_PHONE
     });
     #endif
 }
예제 #8
0
		protected HttpResponse CreateHttpResponse(HttpStatusCode statusCode, string location)
		{
			var encoding = Encoding.UTF8;
			var response = new HttpResponse
			{
				StatusCode = statusCode,
				ContentEncoding = "UTF8",
				ContentLength = 0,
				RawBytes = encoding.GetBytes(""),
				ContentType = "application/json",
			};

			if (location != null)
			{
				response.Headers.Add(new HttpHeader
				{
					Name = "Location",
					Value = location
				});
			}

			return response;
		}
예제 #9
0
 private static void ExecuteCallback(HttpResponse response, Action <HttpResponse> callback)
 {
     PopulateErrorForIncompleteResponse(response);
     callback(response);
 }
예제 #10
0
        private HttpResponse PerformGetResponse(string method)
        {
            var response = new HttpResponse();
            response.ResponseStatus = ResponseStatus.None;

            try
            {
                Assert.InRange(CurrentCallNumber, 0, GetResponses.Count - 1);
                var r = GetResponses[CurrentCallNumber](method);
                CurrentCallNumber++;

                var bytes = r.Raw ? (byte[])r.ResponseObject : Encoding.UTF8.GetBytes(new BaseXmlSerializer().Serialize(r.ResponseObject));
                response.ResponseStatus = ResponseStatus.Completed;
                response.StatusCode = r.StatusCode;
                response.ContentType = r.ContentType ?? "text/xml";
                response.ContentLength = bytes.Length;
                response.RawBytes = bytes;
            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
                response.ErrorException = ex;
                response.ResponseStatus = ResponseStatus.Error;
            }

            Reset();

            return response;
        }
예제 #11
0
 private static void ExecuteCallback(HttpResponse response, Action <HttpResponse> callback)
 {
     callback(response);
 }
예제 #12
0
        public HttpResponse DeleteService(string serviceName)
        {
            XmlNode serviceNode = xmlDoc.SelectSingleNode("ServiceConfig/Service[@Name='" + serviceName  + "']");
            XmlNode keyNode = xmlDoc.SelectSingleNode("ServiceConfig/ApiKeys/Key[@Name='" + serviceName + "']");

            if (serviceNode != null)
            {
                serviceNode.ParentNode.RemoveChild(serviceNode);                
            }

            if (keyNode != null)
            {
                apiKeysNode.RemoveChild(keyNode);
            }

            xmlDoc.Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs/ServiceConfig.xml")); 

            var res = new HttpResponse();
            res.StatusCode = HttpStatusCode.OK;
            return res; ;
        }
예제 #13
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);
                }
            }
        }
예제 #14
0
		private void ResponseCallback(IAsyncResult result, Action<HttpResponse> callback)
		{
			var response = new HttpResponse {ResponseStatus = ResponseStatus.None};

			try
			{
				if(_timeoutState.TimedOut)
				{
					response.ResponseStatus = ResponseStatus.TimedOut;
					ExecuteCallback(response, callback);
					return;
				}

				GetRawResponseAsync(result, webResponse =>
				{
					ExtractResponseData(response, webResponse);
					ExecuteCallback(response, callback);
				});
			}
			catch(Exception ex)
			{
				ExecuteCallback(CreateErrorResponse(ex), callback);
			}
		}
예제 #15
0
        private void GetRawResponseAsync(IAsyncResult result, Action<HttpWebResponse> callback)
        {
            var response = new HttpResponse();
            response.ResponseStatus = ResponseStatus.None;

            HttpWebResponse raw = null;

            try
            {
                var webRequest = (HttpWebRequest)result.AsyncState;
                raw = webRequest.EndGetResponse(result) as HttpWebResponse;
            }
            catch (WebException ex)
            {
                if (ex.Response is HttpWebResponse)
                {
                    raw = ex.Response as HttpWebResponse;
                }
            }

            callback(raw);
            raw.Close();
        }
예제 #16
0
        private void ProcessResponse(IRestRequest request, HttpResponse httpResponse, RestRequestAsyncHandle asyncHandle, Action <IRestResponse, RestRequestAsyncHandle> callback)
        {
            RestResponse arg = ConvertToRestResponse(request, httpResponse);

            callback(arg, asyncHandle);
        }
예제 #17
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);
     }
 }
예제 #18
0
		protected HttpResponse CreateGetResponse(Uri url, HttpStatusCode statusCode, string fileName)
		{
			var encoding = Encoding.UTF8;
			var content = File.ReadAllText(fileName, encoding);
			var response = new HttpResponse
			{
				ResponseUri = url,
				ContentEncoding = "UTF8",
				StatusCode = statusCode,
				ContentLength = content.Length,
				RawBytes = encoding.GetBytes(content),
				ContentType = "application/json",
			};

			return response;
		}
예제 #19
0
 private void PutPostInternalAsync(string method, Action<HttpResponse> callback)
 {
     try
     {
         var webRequest = ConfigureAsyncWebRequest(method, Url);
         PreparePostBody(webRequest);
         WriteRequestBodyAsync(webRequest, callback);
     }
     catch (Exception ex)
     {
         var response = new HttpResponse();
         response.ErrorMessage = ex.Message;
         response.ErrorException = ex;
         response.ResponseStatus = ResponseStatus.Error;
         ExecuteCallback(response, callback);
     }
 }
예제 #20
0
파일: Http.cs 프로젝트: stgwilli/RestSharp
        private void ExtractResponseData(HttpResponse response, HttpWebResponse webResponse)
        {
            using (webResponse)
            {
            #if FRAMEWORK
                response.ContentEncoding = webResponse.ContentEncoding;
                response.Server = webResponse.Server;
            #endif
                response.ContentType = webResponse.ContentType;
                response.ContentLength = webResponse.ContentLength;
                response.RawBytes = webResponse.GetResponseStream().ReadAsBytes();
                response.Content = GetString(response.RawBytes);
                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 { Name = cookie.Name, Value = cookie.Value });
                    }
                }

                foreach (var headerName in webResponse.Headers.AllKeys)
                {
                    var headerValue = webResponse.Headers[headerName];
                    response.Headers.Add(new HttpHeader { Name = headerName, Value = headerValue });
                }

                webResponse.Close();
            }
        }
예제 #21
0
        private void ResponseCallback(IAsyncResult result, Action<HttpResponse> callback)
        {
            var response = new HttpResponse();
            response.ResponseStatus = ResponseStatus.None;

            try
            {
                if (timeoutState.TimedOut)
                {
                    response.ResponseStatus = ResponseStatus.TimedOut;
                    ExecuteCallback(response, callback);
                    return;
                }

                GetRawResponseAsync(result, webResponse =>
                {
                    ExtractResponseData(response, webResponse);
                    ExecuteCallback(response, callback);
                });
            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
                response.ErrorException = ex;
                response.ResponseStatus = ResponseStatus.Error;
                ExecuteCallback(response, callback);
            }
        }
예제 #22
0
        private HttpResponse GetResponse(HttpWebRequest request)
        {
            HttpResponse response = new HttpResponse { ResponseStatus = ResponseStatus.None };

            try
            {
                HttpWebResponse webResponse = GetRawResponse(request);

                this.ExtractResponseData(response, webResponse);
            }
            catch (Exception ex)
            {
                this.ExtractErrorResponse(response, ex);
            }

            return response;
        }
예제 #23
0
        // stripped down version of rest-sharp implementation
        private HttpResponse GetResponse(WebRequest request)
        {
            var response = new HttpResponse { ResponseStatus = ResponseStatus.None };

            try
            {
                var webResponse = GetRawResponse(request);
                ExtractResponseData(response, webResponse);
            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
                response.ErrorException = ex;
                response.ResponseStatus = ResponseStatus.Error;
            }

            return response;
        }
예제 #24
0
        private async Task<HttpResponse> GetResponse(HttpClient httpClient, HttpRequestMessage httpRequestMessage)
		{
			var response = new HttpResponse();
			response.ResponseStatus = ResponseStatus.None;

			try
			{
                var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage);

                //TODO: Move the exception handling currently in GetRawResponse here and check Result

                ExtractResponseData(response, httpResponseMessage);
            }
			catch (Exception ex)
			{
				response.ErrorMessage = ex.Message;
				response.ErrorException = ex;
				response.ResponseStatus = ResponseStatus.Error;
			}

			return response;
		}
예제 #25
0
        //private static void WriteStringTo(Stream stream, string toWrite)
        //{
        //    var bytes = _defaultEncoding.GetBytes(toWrite);
        //    stream.Write(bytes, 0, bytes.Length);
        //}
		
        private static void ExtractResponseData(HttpResponse response, HttpResponseMessage httpResponseMessage)
		{
            using (httpResponseMessage)
            {
                //response.ContentEncoding = httpResponseMessage.Content.Headers.ContentEncoding;
                //response.Server = httpResponseMessage.Headers.Server;
                response.ContentType = httpResponseMessage.Content.Headers.ContentType.MediaType;
                response.ContentLength = httpResponseMessage.Content.Headers.ContentLength.Value;

                response.RawBytes = httpResponseMessage.Content.ReadAsByteArrayAsync().Result;

                // NOTE: Commented out in original code
                //response.Content = GetString(response.RawBytes);

                response.StatusCode = (int)httpResponseMessage.StatusCode;
                response.StatusDescription = httpResponseMessage.ReasonPhrase;
                //response.ResponseUri = httpResponseMessage
                response.ResponseStatus = ResponseStatus.Completed;

                // cookies

                foreach (var header in httpResponseMessage.Headers.ToArray() )
                {
                    //var headerValue = httpResponseMessage.Headers.GetValues(header.Key);
                    //response.Headers.Add(new HttpHeader { Name = header.Key, Value = header.Value });
                }
            }

            #region Original Code [REMOVE]
            //TODO: Remove this code once we resolved all conflicts
            //using (webResponse)
            //{
            //    response.ContentEncoding = webResponse.ContentEncoding;
            //    response.Server = webResponse.Server;
            //    response.ContentType = webResponse.ContentType;
            //    response.ContentLength = webResponse.ContentLength;

            //    response.RawBytes = webResponse.GetResponseStream().ReadAsBytes();

            //    //response.Content = GetString(response.RawBytes);

            //    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 (var headerName in webResponse.Headers.AllKeys)
            //    {
            //        var headerValue = webResponse.Headers[headerName];
            //        response.Headers.Add(new HttpHeader { Name = headerName, Value = headerValue });
            //    }

            //    webResponse.Close();
            //}
            #endregion
        }
예제 #26
0
 private void ProcessResponseStream(Stream webResponseStream, HttpResponse response)
 {
     if (ResponseWriter == null)
     {
         response.RawBytes = webResponseStream.ReadAsBytes();
     }
     else
     {
         ResponseWriter(webResponseStream);
     }
 }
예제 #27
0
        private void RequestStreamCallback(IAsyncResult result, Action<HttpResponse> callback)
        {
            HttpWebRequest webRequest = (HttpWebRequest) result.AsyncState;

            if (this.timeoutState.TimedOut)
            {
                HttpResponse response = new HttpResponse { ResponseStatus = ResponseStatus.TimedOut };

                ExecuteCallback(response, callback);

                return;
            }

            // write body to request stream
            try
            {
                using (Stream requestStream = webRequest.EndGetRequestStream(result))
                {
                    if (this.HasFiles || this.AlwaysMultipartFormData)
                    {
                        this.WriteMultipartFormData(requestStream);
                    }
                    else if (this.RequestBodyBytes != null)
                    {
                        requestStream.Write(this.RequestBodyBytes, 0, this.RequestBodyBytes.Length);
                    }
                    else if (this.RequestBody != null)
                    {
                        this.WriteStringTo(requestStream, this.RequestBody);
                    }
                }
            }
            catch (Exception ex)
            {
                ExecuteCallback(this.CreateErrorResponse(ex), callback);

                return;
            }

            IAsyncResult asyncResult = webRequest.BeginGetResponse(r => this.ResponseCallback(r, callback), webRequest);

            this.SetTimeout(asyncResult, this.timeoutState);
        }
예제 #28
0
		private HttpResponse CreateErrorResponse(Exception ex)
		{
			var response = new HttpResponse();
			var webException = ex as WebException;
			if (webException != null && webException.Status == WebExceptionStatus.RequestCanceled)
			{
				response.ResponseStatus = _timeoutState.TimedOut ? ResponseStatus.TimedOut : ResponseStatus.Aborted;
				return response;
			}

			response.ErrorMessage = ex.Message;
			response.ErrorException = ex;
			response.ResponseStatus = ResponseStatus.Error;
			return response;
		}
예제 #29
0
 private static void ExecuteCallback(HttpResponse response, Action<HttpResponse> callback)
 {
     PopulateErrorForIncompleteResponse(response);
     callback(response);
 }
예제 #30
0
		private static void GetRawResponseAsync(IAsyncResult result, Action<HttpWebResponse> callback)
		{
			var response = new HttpResponse();
			response.ResponseStatus = ResponseStatus.None;

			HttpWebResponse raw = null;

            try
            {
                var webRequest = (HttpWebRequest)result.AsyncState;
                raw = webRequest.EndGetResponse(result) as HttpWebResponse;
            }
            catch(WebException ex)
            {
                if(ex.Status == WebExceptionStatus.RequestCanceled)
                {
                    throw ex;
                }

                // 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 (ex.Response is HttpWebResponse)
                {
                    raw = ex.Response as HttpWebResponse;
                }
                else
                {
                    throw ex;
                }
            }

			callback(raw);
			raw.Close();
		}
예제 #31
0
 private static void PopulateErrorForIncompleteResponse(HttpResponse response)
 {
     if (response.ResponseStatus != ResponseStatus.Completed && response.ErrorException == null)
     {
         response.ErrorException = response.ResponseStatus.ToWebException();
         response.ErrorMessage = response.ErrorException.Message;
     }
 }
예제 #32
0
		private static void ExecuteCallback(HttpResponse response, Action<HttpResponse> callback)
		{
			callback(response);
		}
예제 #33
0
        private static void ExtractResponseData(HttpResponse response, HttpWebResponse webResponse)
        {
            using (webResponse)
            {
            #if FRAMEWORK
                response.ContentEncoding = webResponse.ContentEncoding;
                response.Server = webResponse.Server;
            #endif
                response.ContentType = webResponse.ContentType;
                response.ContentLength = webResponse.ContentLength;

            #if WINDOWS_PHONE71
                if (webResponse.Headers[HttpRequestHeader.ContentEncoding] == "gzip")
                {
                    var uncompressedResponse = new SharpGIS.GZipWebClient.GZipWebResponse(webResponse);
                    response.RawBytes = uncompressedResponse.GetResponseStream().ReadAsBytes();
                }
                else
                {
                    response.RawBytes = webResponse.GetResponseStream().ReadAsBytes();
                }
            #else
                response.RawBytes = webResponse.GetResponseStream().ReadAsBytes();
            #endif

                //response.Content = GetString(response.RawBytes);
                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 (var headerName in webResponse.Headers.AllKeys)
                {
                    var headerValue = webResponse.Headers[headerName];
                    response.Headers.Add(new HttpHeader { Name = headerName, Value = headerValue });
                }
            #if !NETFX_CORE
                webResponse.Close();
            #endif
            }
        }