private static HttpClient CreateHttpClient(IClientConfig clientConfig) { var httpMessageHandler = new HttpClientHandler(); // If HttpClientHandler.AllowAutoRedirect is set to true (default value), // redirects for GET requests are automatically followed and redirects for POST // requests are thrown back as exceptions. // If HttpClientHandler.AllowAutoRedirect is set to false (e.g. S3), // redirects are returned as responses. httpMessageHandler.AllowAutoRedirect = clientConfig.AllowAutoRedirect; var proxy = clientConfig.GetWebProxy(); if (proxy != null) { httpMessageHandler.Proxy = proxy; } if (httpMessageHandler.Proxy != null && clientConfig.ProxyCredentials != null) { httpMessageHandler.Proxy.Credentials = clientConfig.ProxyCredentials; } var httpClient = new HttpClient(httpMessageHandler); if (clientConfig.Timeout.HasValue) { // Timeout value is set to ClientConfig.MaxTimeout for S3 and Glacier. // Use default value (100 seconds) for other services. httpClient.Timeout = clientConfig.Timeout.Value; } return(httpClient); }
public override HttpClient CreateHttpClient(IClientConfig clientConfig) { var httpMessageHandler = CreateClientHandler(); //Adds the client certificate to the Http message handler httpMessageHandler.ClientCertificates.Add(LoadClientCertificateCredentials()); if (clientConfig.MaxConnectionsPerServer.HasValue) { httpMessageHandler.MaxConnectionsPerServer = clientConfig.MaxConnectionsPerServer.Value; } httpMessageHandler.AllowAutoRedirect = clientConfig.AllowAutoRedirect; var proxy = clientConfig.GetWebProxy(); if (proxy != null) { httpMessageHandler.Proxy = proxy; } if (httpMessageHandler.Proxy != null && clientConfig.ProxyCredentials != null) { httpMessageHandler.Proxy.Credentials = clientConfig.ProxyCredentials; } var httpClient = new HttpClient(httpMessageHandler); if (clientConfig.Timeout.HasValue) { httpClient.Timeout = clientConfig.Timeout.Value; } return(httpClient); }
public override HttpClient CreateHttpClient(IClientConfig clientConfig) { var httpMessageHandler = CreateClientHandler(); if (clientConfig.MaxConnectionsPerServer.HasValue) { httpMessageHandler.MaxConnectionsPerServer = clientConfig.MaxConnectionsPerServer.Value; } httpMessageHandler.AllowAutoRedirect = clientConfig.AllowAutoRedirect; // Disable automatic decompression when Content-Encoding header is present httpMessageHandler.AutomaticDecompression = DecompressionMethods.None; var proxy = clientConfig.GetWebProxy(); if (proxy != null) { httpMessageHandler.Proxy = proxy; } if (httpMessageHandler.Proxy != null && clientConfig.ProxyCredentials != null) { httpMessageHandler.Proxy.Credentials = clientConfig.ProxyCredentials; } var httpClient = new HttpClient(httpMessageHandler); if (clientConfig.Timeout.HasValue) { // Timeout value is set to ClientConfig.MaxTimeout for S3 and Glacier. // Use default value (100 seconds) for other services. httpClient.Timeout = clientConfig.Timeout.Value; } return(httpClient); }
public override HttpClient CreateHttpClient(IClientConfig clientConfig) { var httpMessageHandler = CreateClientHandler(); if (clientConfig.MaxConnectionsPerServer.HasValue) { httpMessageHandler.MaxConnectionsPerServer = clientConfig.MaxConnectionsPerServer.Value; } httpMessageHandler.AllowAutoRedirect = clientConfig.AllowAutoRedirect; httpMessageHandler.AutomaticDecompression = DecompressionMethods.None; var proxy = clientConfig.GetWebProxy(); if (proxy != null) { httpMessageHandler.Proxy = proxy; } if (httpMessageHandler.Proxy != null && clientConfig.ProxyCredentials != null) { httpMessageHandler.Proxy.Credentials = clientConfig.ProxyCredentials; } var httpClient = new HttpClient(httpMessageHandler); if (clientConfig.Timeout.HasValue) { httpClient.Timeout = clientConfig.Timeout.Value; } return(httpClient); }
private static IWebProxy GetProxyIfAvailableAndConfigured(IClientConfig config) { #if BCL || UNITY || CORECLR return(config.GetWebProxy()); #else return(null); #endif }
/// <summary> /// If proxy or proxy credentials are set this returns false because those properties can't be /// serialized as part of the key in the global http client cache. /// </summary> private static bool CanClientConfigBeSerialized(IClientConfig clientConfig) { #if CORECLR return(clientConfig.ProxyCredentials == null && clientConfig.GetWebProxy() == null); #else return(clientConfig.ProxyCredentials == null); #endif }
/// <summary> /// Create and configure a managed HttpClient instance. /// The use of HttpClientHandler in the constructor for HttpClient implicitly creates a managed HttpClient. /// </summary> /// <param name="clientConfig"></param> /// <returns></returns> private static HttpClient CreateManagedHttpClient(IClientConfig clientConfig) { var httpMessageHandler = new HttpClientHandler(); if (clientConfig.MaxConnectionsPerServer.HasValue) { httpMessageHandler.MaxConnectionsPerServer = clientConfig.MaxConnectionsPerServer.Value; } try { // If HttpClientHandler.AllowAutoRedirect is set to true (default value), // redirects for GET requests are automatically followed and redirects for POST // requests are thrown back as exceptions. // If HttpClientHandler.AllowAutoRedirect is set to false (e.g. S3), // redirects are returned as responses. httpMessageHandler.AllowAutoRedirect = clientConfig.AllowAutoRedirect; // Disable automatic decompression when Content-Encoding header is present httpMessageHandler.AutomaticDecompression = DecompressionMethods.None; } catch (PlatformNotSupportedException pns) { Logger.GetLogger(typeof(HttpRequestMessageFactory)).Debug(pns, $"The current runtime does not support modifying the configuration of HttpClient."); } try { var proxy = clientConfig.GetWebProxy(); if (proxy != null) { httpMessageHandler.Proxy = proxy; } if (httpMessageHandler.Proxy != null && clientConfig.ProxyCredentials != null) { httpMessageHandler.Proxy.Credentials = clientConfig.ProxyCredentials; } } catch (PlatformNotSupportedException pns) { Logger.GetLogger(typeof(HttpRequestMessageFactory)).Debug(pns, $"The current runtime does not support modifying proxy settings of HttpClient."); } var httpClient = new HttpClient(httpMessageHandler); if (clientConfig.Timeout.HasValue) { // Timeout value is set to ClientConfig.MaxTimeout for S3 and Glacier. // Use default value (100 seconds) for other services. httpClient.Timeout = clientConfig.Timeout.Value; } return(httpClient); }
/// <summary> /// Determines if HttpClients created with the given IClientConfig should be cached at the SDK /// client level, or cached globally. /// /// If there is no HttpClientFactory assigned and proxy or proxy credentials are set /// this returns false because those properties can't be serialized as part of the key in the global http client cache. /// </summary> internal static bool UseGlobalHttpClientCache(IClientConfig clientConfig) { if (clientConfig.HttpClientFactory == null) { return(clientConfig.ProxyCredentials == null && clientConfig.GetWebProxy() == null); } else { return(clientConfig.HttpClientFactory.GetConfigUniqueString(clientConfig) != null); } return(clientConfig.ProxyCredentials == null); }
/// <summary> /// Create and configure a managed HttpClient instance. /// The use of HttpClientHandler in the constructor for HttpClient implicitly creates a managed HttpClient. /// </summary> /// <param name="clientConfig"></param> /// <returns></returns> private static HttpClient CreateManagedHttpClient(IClientConfig clientConfig) { var httpMessageHandler = new HttpClientHandler(); #if NETSTANDARD if (clientConfig.MaxConnectionsPerServer.HasValue) { httpMessageHandler.MaxConnectionsPerServer = clientConfig.MaxConnectionsPerServer.Value; } #endif // If HttpClientHandler.AllowAutoRedirect is set to true (default value), // redirects for GET requests are automatically followed and redirects for POST // requests are thrown back as exceptions. // If HttpClientHandler.AllowAutoRedirect is set to false (e.g. S3), // redirects are returned as responses. httpMessageHandler.AllowAutoRedirect = clientConfig.AllowAutoRedirect; // Disable automatic decompression when Content-Encoding header is present httpMessageHandler.AutomaticDecompression = DecompressionMethods.None; var proxy = clientConfig.GetWebProxy(); if (proxy != null) { httpMessageHandler.Proxy = proxy; } if (httpMessageHandler.Proxy != null && clientConfig.ProxyCredentials != null) { httpMessageHandler.Proxy.Credentials = clientConfig.ProxyCredentials; } var httpClient = new HttpClient(httpMessageHandler); if (clientConfig.Timeout.HasValue) { // Timeout value is set to ClientConfig.MaxTimeout for S3 and Glacier. // Use default value (100 seconds) for other services. httpClient.Timeout = clientConfig.Timeout.Value; } return(httpClient); }
private static IWebProxy GetProxyIfAvailableAndConfigured(IClientConfig config) { return(config.GetWebProxy()); }
private static IWebProxy GetProxyIfAvailableAndConfigured(IClientConfig config) { #if BCL || UNITY || CORECLR return config.GetWebProxy(); #else return null; #endif }