예제 #1
0
파일: Http.cs 프로젝트: RestRT/RestRT
        //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;

                if (httpResponseMessage.Content.Headers.ContentType != null)
                {
                    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
        }
예제 #2
0
파일: Http.Sync.cs 프로젝트: RestRT/RestRT
        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;
		}
예제 #3
0
파일: RestClient.cs 프로젝트: RestRT/RestRT
        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;
        }