Ejemplo n.º 1
0
        internal virtual HttpWebRequest CreateWebRequest(HttpRequestMessage request)
        {
            var wr = new HttpWebRequest(request.RequestUri);

            wr.ThrowOnError = false;
            wr.AllowWriteStreamBuffering = false;

            wr.ConnectionGroupName = connectionGroupName;
            wr.Method          = request.Method.Method;
            wr.ProtocolVersion = request.Version;

            if (wr.ProtocolVersion == HttpVersion.Version10)
            {
                wr.KeepAlive = request.Headers.ConnectionKeepAlive;
            }
            else
            {
                wr.KeepAlive = request.Headers.ConnectionClose != true;
            }

            wr.ServicePoint.Expect100Continue = request.Headers.ExpectContinue == true;

            if (allowAutoRedirect)
            {
                wr.AllowAutoRedirect            = true;
                wr.MaximumAutomaticRedirections = maxAutomaticRedirections;
            }
            else
            {
                wr.AllowAutoRedirect = false;
            }

            wr.AutomaticDecompression = automaticDecompression;
            wr.PreAuthenticate        = preAuthenticate;

            if (useCookies)
            {
                // It cannot be null or allowAutoRedirect won't work
                wr.CookieContainer = CookieContainer;
            }

            if (useDefaultCredentials)
            {
                wr.UseDefaultCredentials = true;
            }
            else
            {
                wr.Credentials = credentials;
            }

            if (useProxy)
            {
                wr.Proxy = proxy;
            }

            // Add request headers
            var headers = wr.Headers;

            foreach (var header in request.Headers)
            {
                headers.AddValue(header.Key, HttpRequestHeaders.GetSingleHeaderString(header.Key, header.Value));
            }

            return(wr);
        }
Ejemplo n.º 2
0
        internal virtual HttpWebRequest CreateWebRequest(HttpRequestMessage request)
        {
            var wr = new HttpWebRequest(request.RequestUri);

            wr.ThrowOnError = false;
            wr.AllowWriteStreamBuffering = false;

            wr.ConnectionGroupName = connectionGroupName;
            wr.Method          = request.Method.Method;
            wr.ProtocolVersion = request.Version;

            if (wr.ProtocolVersion == HttpVersion.Version10)
            {
                wr.KeepAlive = request.Headers.ConnectionKeepAlive;
            }
            else
            {
                wr.KeepAlive = request.Headers.ConnectionClose != true;
            }

            wr.ServicePoint.Expect100Continue = request.Headers.ExpectContinue == true;

            if (allowAutoRedirect)
            {
                wr.AllowAutoRedirect            = true;
                wr.MaximumAutomaticRedirections = maxAutomaticRedirections;
            }
            else
            {
                wr.AllowAutoRedirect = false;
            }

            wr.AutomaticDecompression = automaticDecompression;
            wr.PreAuthenticate        = preAuthenticate;

            if (useCookies)
            {
                // It cannot be null or allowAutoRedirect won't work
                wr.CookieContainer = CookieContainer;
            }

            if (useDefaultCredentials)
            {
                wr.UseDefaultCredentials = true;
            }
            else
            {
                wr.Credentials = credentials;
            }

            if (useProxy)
            {
                wr.Proxy = proxy;
            }

            // Add request headers
            var headers = wr.Headers;

            foreach (var header in request.Headers)
            {
                var values = header.Value;
                if (header.Key == "Host")
                {
                    //
                    // Host must be explicitly set for HttpWebRequest
                    //
                    wr.Host = request.Headers.Host;
                    continue;
                }

                if (header.Key == "Transfer-Encoding")
                {
                    // Chunked Transfer-Encoding is never set for HttpWebRequest. It's detected
                    // from ContentLength by HttpWebRequest
                    values = values.Where(l => l != "chunked");
                }

                var values_formated = HttpRequestHeaders.GetSingleHeaderString(header.Key, values);
                if (values_formated == null)
                {
                    continue;
                }

                headers.AddValue(header.Key, values_formated);
            }

            return(wr);
        }
Ejemplo n.º 3
0
 internal static string GetSingleHeaderString(string name, IEnumerable <string> values)
 {
     return(HttpRequestHeaders.GetSingleHeaderString(name, values));
 }