Пример #1
0
        private static HttpMessageHandler ValueFactory(HttpHandlerOptions handlerOptions)
        {
            var socketsHandler = new SocketsHttpHandler
            {
                PooledConnectionLifetime    = TimeSpan.FromSeconds(60),
                PooledConnectionIdleTimeout = TimeSpan.FromMinutes(20),
                MaxConnectionsPerServer     = 2
            };

            if (handlerOptions.Proxy != null)
            {
                socketsHandler.Proxy = handlerOptions.Proxy;
            }

            if (handlerOptions.IgnoreProxy)
            {
                socketsHandler.UseProxy = false;
            }

            if (handlerOptions.ClientCertificates != null)
            {
                foreach (var handlerOptionsClientCertificate in handlerOptions.ClientCertificates)
                {
                    if (socketsHandler.SslOptions.ClientCertificates == null)
                    {
                        socketsHandler.SslOptions.ClientCertificates = new X509CertificateCollection();
                    }
                    socketsHandler.SslOptions.ClientCertificates.Add(handlerOptionsClientCertificate);
                }
            }



            return(socketsHandler);
        }
Пример #2
0
        public HttpRequestMessage BuildHttpRequestMessage(HttpHandlerOptions httpHandlerOptions, HttpMethod httpMethod, object?content)
        {
            var requestUri = !PathSegments.Any()
                ? httpHandlerOptions.RequestUri
                : new Uri(httpHandlerOptions.RequestUri, String.Join('/', PathSegments));



            if (QueryParameters.Any())
            {
                var qb = new QueryBuilder();
                if (!String.IsNullOrWhiteSpace(requestUri.Query))
                {
                    var query = QueryHelpers.ParseQuery(requestUri.Query);
                    foreach (var kv in query)
                    {
                        qb.Add(kv.Key, kv.Value.ToArray());
                    }
                }
                foreach (var kv in QueryParameters)
                {
                    qb.Add(kv.Key, kv.Value.ToArray());
                }

                var uriBuilder = new UriBuilder(requestUri);
                uriBuilder.Query = qb.ToQueryString().ToString();

                requestUri = uriBuilder.Uri;
            }



            var message = new HttpRequestMessage(httpMethod, requestUri);

            if (content != null)
            {
                message.Content = CreateHttpContent(content);
            }

            if (Headers.Any())
            {
                foreach (var kv in Headers)
                {
                    if (message.Headers.Contains(kv.Key))
                    {
                        message.Headers.Remove(kv.Key);
                    }
                    message.Headers.Add(kv.Key, kv.Value);
                }
            }


            if (!String.IsNullOrWhiteSpace(ContentType) && message.Content != null)
            {
                message.Content.Headers.ContentType = new MediaTypeHeaderValue(ContentType);
            }

            return(message);
        }
Пример #3
0
        public static HttpMessageHandler Build(HttpHandlerOptions handlerOptions)
        {
            var roHttpHandlerOptions = new ReadonlyHttpHandlerOptions(handlerOptions);

            return(HttpHandlers.GetOrAdd(roHttpHandlerOptions, _ => ValueFactory(handlerOptions)));
        }
Пример #4
0
 public HttpRequestBuilder(HttpHandlerOptions httpHandlerOptions)
 {
     _httpHandlerOptions = httpHandlerOptions;
 }
Пример #5
0
 public HttpOptionsBuilder(string url)
 {
     _httpHandlerOptions = new HttpHandlerOptions(UriHelper.BuildUri(url));
 }
Пример #6
0
 public HttpRequestBuilder Client(string url, HttpHandlerOptions options)
 {
     return(new HttpRequestBuilder(options));
 }
 public ReadonlyHttpHandlerOptions(HttpHandlerOptions httpHandlerOptions)
 {
     DestinationHost = httpHandlerOptions.RequestUri.Host;
     ProxyHost       = httpHandlerOptions.Proxy?.Address?.Host;
     IgnoreProxy     = httpHandlerOptions.IgnoreProxy;
 }