public async Task <IHttpWebResponse> PerformAsync()
        {
            var webReq = await _createRequestFuncAsync(null);

            System.Net.HttpWebResponse result;

            try
            {
                result = (System.Net.HttpWebResponse) await Task <WebResponse> .Factory.FromAsync(webReq.BeginGetResponse, webReq.EndGetResponse, webReq);

                return(new HttpWebResponse(result));
            }
            catch (WebException e)
            {
                result = (System.Net.HttpWebResponse)e.Response;

                if (!_negotiate)
                {
                    return(new HttpWebResponse(result));
                }
            }

            var accept = result.Headers["Accept"];

            webReq = await _createRequestFuncAsync(HttpConverter.ContentType(accept));

            result = (System.Net.HttpWebResponse) await Task <WebResponse> .Factory.FromAsync(webReq.BeginGetResponse, webReq.EndGetResponse, webReq);

            return(new HttpWebResponse(result));
        }
Beispiel #2
0
        private IEnumerable <ContentType> GetContentTypeFromAcceptHeader(IHttpWebResponse rawResponse)
        {
            var acceptHeader = rawResponse.Headers["Accept"];

            if (acceptHeader == null)
            {
                yield break;
            }

            var headers = _mapper.Map(acceptHeader);

            foreach (var header in headers)
            {
                yield return(HttpConverter.ContentTypeOrDefault(header.ContentType));
            }
        }
        public RequestWrapper Prepare()
        {
            Func <ContentType?, Task <HttpWebRequest> > requestCreateFunc = async(ContentType? overrideType) =>
            {
                string type;
                if (overrideType.HasValue)
                {
                    type = HttpConverter.ContentType(overrideType.Value);
                }
                else
                {
                    type = HttpConverter.ContentType(_contentType);
                }

                var serializer = _serializers.FirstOrDefault(s => s.ContentType.Contains(_contentType));
                if (serializer == null)
                {
                    throw new Exception(string.Format("Can not find serializer for content type '{0}'.", _contentType));
                }

                var webReq = (HttpWebRequest)WebRequest.Create(_url);
                webReq.AllowAutoRedirect = true;
                webReq.UserAgent         = ClientUtils.GetUserAgent();
                webReq.Method            = _method;
                webReq.Accept            = _accept;

                if (_data != null)
                {
                    _data = serializer.Serialize(_data);
                    webReq.ContentType = type;
                    var payloadAsBytes = Encoding.UTF8.GetBytes(_data.ToString());
                    webReq.ContentLength = payloadAsBytes.Length;

                    using (var stream = await Task <Stream> .Factory.FromAsync(webReq.BeginGetRequestStream, webReq.EndGetRequestStream, webReq))
                    {
                        await stream.WriteAsync(payloadAsBytes, 0, payloadAsBytes.Length);
                    }
                }
                return(webReq);
            };

            return(new RequestWrapper(requestCreateFunc, _negotiate));
        }
        private ContentType ExtractContentTypeFromHeaders(WebHeaderCollection headers)
        {
            var defaultContentType = ContentType.ApplicationJson;

            if (headers == null)
            {
                return(defaultContentType);
            }

            var contentTypeHeader = headers[HttpResponseHeader.ContentType];

            if (contentTypeHeader == null)
            {
                return(defaultContentType);
            }

            var contentType = HttpConverter.ContentType(contentTypeHeader);

            return(contentType);
        }
 public FluentWebRequestBuilder WithMethod(HttpMethod method)
 {
     _method = HttpConverter.HttpMethod(method);
     return(this);
 }