Exemplo n.º 1
0
        private HttpResponseMessage CreateResponse(HttpRequestMessage request, HttpWebResponse httpResponse)
        {
            HttpResponseMessage response;
            response = new HttpResponseMessage(httpResponse.StatusCode);
            response.Request = request;
            response.CopyHeadersFrom(httpResponse.Headers);
            response.StatusCode = httpResponse.StatusCode;

            MemoryStream memory;
            var contentLength = response.Headers[HttpResponseHeader.ContentLength];
            int contentIntLength;
            if (int.TryParse(contentLength, out contentIntLength))
            {
                memory = new MemoryStream(contentIntLength);
            }
            else
            {
                memory = new MemoryStream();
            }

            var responseStream = httpResponse.GetResponseStream();
            responseStream.CopyTo(memory);
            memory.Seek(0L, SeekOrigin.Begin);

            response.Content = new HttpContent(memory);
            return response;
        }
Exemplo n.º 2
0
        public HttpResponseMessage SendAsync(HttpRequestMessage request)
        {
            var httpRequest = (HttpWebRequest)HttpWebRequest.Create(request.RequestUri);
            httpRequest.Method = request.Method.ToString().ToUpperInvariant();

            this.SetHeaders(this.defaultRequestHeaders, httpRequest);
            this.SetHeaders(request.Headers, httpRequest);

            if (request.Content != null)
            {
                this.SetHeaders(request.Content.Headers, httpRequest);

                var bytes = request.Content.GetBytes();
                if (bytes.Length > 0)
                {
                    var requestStream = httpRequest.GetRequestStream();
                    requestStream.Write(bytes, 0, bytes.Length);
                    requestStream.Flush();
                }
            }

            HttpWebResponse httpResponse;
            HttpResponseMessage response;
            try
            {
                httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                response = this.CreateResponse(request, httpResponse);
                return response;
            }
            catch (WebException ex)
            {
                httpResponse = (HttpWebResponse)ex.Response;
                response = this.CreateResponse(request, httpResponse);
                return response;
            }
        }