示例#1
0
 private void ApplyServerCertificateValidator(HttpRequestInfo value, HttpClientHandler clientHandler)
 {
     if (value.ServerCertificateValidator != null)
     {
         clientHandler.ServerCertificateCustomValidationCallback = value.ServerCertificateValidator.IsOk;
     }
 }
示例#2
0
 private void ApplyProxy(HttpRequestInfo value, HttpClientHandler clientHandler)
 {
     if (value.Proxy != null)
     {
         clientHandler.Proxy    = value.Proxy;
         clientHandler.UseProxy = true;
     }
 }
示例#3
0
 private void ApplyCookies(HttpRequestInfo value, HttpClientHandler clientHandler)
 {
     if (value.Cookies?.Count > 0)
     {
         foreach (Cookie cookie in value.Cookies.Where(c => !string.IsNullOrWhiteSpace(c.Domain)))
         {
             clientHandler.CookieContainer.Add(cookie);
         }
     }
 }
示例#4
0
 private void ApplyRedirects(HttpRequestInfo value, HttpClientHandler clientHandler)
 {
     if (value.MaxRedirects > 0)
     {
         clientHandler.AllowAutoRedirect        = true;
         clientHandler.MaxAutomaticRedirections = value.MaxRedirects;
     }
     else
     {
         clientHandler.AllowAutoRedirect = false;
     }
 }
        public static HttpRequestMessage CreateRequestMessage(this HttpRequestInfo info)
        {
            HttpRequestMessage result = new HttpRequestMessage(
                method: new HttpMethod(info.Method),
                requestUri: info.RequestUri.ToString())
                                        .ApplyAcceptEncoding()
                                        .ApplyAcceptContentTypes(info)
                                        .AddHeaders(info)
                                        .ApplyContent(info)
                                        .ApplyUserAgent(info);

            return(result);
        }
示例#6
0
        private void ApplyCertificates(HttpRequestInfo value, HttpClientHandler clientHandler)
        {
            if (value.Certificates?.Count > 0)
            {
                clientHandler.ClientCertificateOptions = ClientCertificateOption.Manual;
                clientHandler.SslProtocols             = SslProtocols.Tls12 | SslProtocols.Tls11;

                foreach (X509Certificate certificate in value.Certificates)
                {
                    clientHandler.ClientCertificates.Add(certificate);
                }
            }
        }
示例#7
0
        private void ApplyAuthentication(HttpRequestInfo value, HttpClientHandler clientHandler)
        {
            if (value.UseDefaultCredentials.HasValue)
            {
                clientHandler.UseDefaultCredentials = value.UseDefaultCredentials.Value;
            }

            if (value.PreAuthenticate.HasValue)
            {
                clientHandler.PreAuthenticate = value.PreAuthenticate.Value;
            }

            if (value.Credentials != null)
            {
                clientHandler.Credentials = value.Credentials;
            }
        }
示例#8
0
        public HttpMessageHandler Create(HttpRequestInfo value)
        {
            value.ArgNotNull(nameof(value));

            HttpClientHandler clientHandler = new HttpClientHandler
            {
                AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip
            };

            ApplyAuthentication(value: value, clientHandler: clientHandler);

            ApplyCertificates(value: value, clientHandler: clientHandler);

            ApplyServerCertificateValidator(value: value, clientHandler: clientHandler);

            ApplyProxy(value: value, clientHandler: clientHandler);

            ApplyRedirects(value: value, clientHandler: clientHandler);

            ApplyCookies(value: value, clientHandler: clientHandler);


            return(clientHandler);
        }
        private static HttpRequestMessage AddHeaders(this HttpRequestMessage request, HttpRequestInfo info)
        {
            if (info.Headers?.Count > 0)
            {
                IEnumerable <IGrouping <string, HttpHeader> > headers = info.Headers.GroupBy(h => h.Name);
                foreach (IGrouping <string, HttpHeader> headerGroup in headers)
                {
                    IEnumerable <string> values = headerGroup.Select(h => h.Value);
                    request.Headers.Add(name: headerGroup.Key, values: values);
                }
            }

            return(request);
        }
        private static HttpRequestMessage ApplyContent(this HttpRequestMessage request, HttpRequestInfo info)
        {
            if (!string.IsNullOrWhiteSpace(info.Content))
            {
                HttpContent content     = new StringContent(content: info.Content, encoding: Encoding.UTF8);
                string      contentType = info.ContentTypes.FirstOrDefault();
                if (contentType != null)
                {
                    content.Headers.ContentType = new MediaTypeWithQualityHeaderValue(contentType);
                }

                request.Content = content;
            }

            return(request);
        }
        private static HttpRequestMessage ApplyUserAgent(this HttpRequestMessage request, HttpRequestInfo info)
        {
            if (!string.IsNullOrEmpty(info.UserAgent))
            {
                request.Headers.UserAgent.ParseAdd(info.UserAgent);
            }

            return(request);
        }
        private static HttpRequestMessage ApplyAcceptContentTypes(this HttpRequestMessage request, HttpRequestInfo info)
        {
            List <string> acceptableContentTypes = new List <string> {
                "application/json", "application/jose+jwe"
            };

            foreach (string contentType in acceptableContentTypes)
            {
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType));
            }

            return(request);
        }