private static HttpClient CreateHttpClient( HttpSettings httpSettings, HttpPolicy httpPolicy, HttpMessageHandler httpMessageHandler = null) { var client = httpMessageHandler == null ? new HttpClient() : new HttpClient(httpMessageHandler); client.BaseAddress = httpSettings.RequestUri; client.Timeout = httpPolicy.Timeout; if (string.IsNullOrWhiteSpace(httpSettings.UserName) || string.IsNullOrWhiteSpace(httpSettings.Password)) { return(client); } var byteArray = Encoding.ASCII.GetBytes($"{httpSettings.UserName}:{httpSettings.Password}"); client.BaseAddress = httpSettings.RequestUri; client.Timeout = httpPolicy.Timeout; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); return(client); }
private static HttpClient CreateHttpClient( HttpSettings httpSettings, HttpPolicy httpPolicy, HttpMessageHandler httpMessageHandler = null) { HttpClientHandler allowInsecureHandler = null; if (httpSettings.AllowInsecureSsl) { allowInsecureHandler = new HttpClientHandler { ClientCertificateOptions = ClientCertificateOption.Manual, ServerCertificateCustomValidationCallback = (httpRequestMessage, cert, cetChain, policyErrors) => { return(true); } }; } HttpClient client = null; if (httpSettings.AllowInsecureSsl) { client = new HttpClient(allowInsecureHandler); } else { client = httpMessageHandler == null ? new HttpClient() : new HttpClient(httpMessageHandler); } client.BaseAddress = httpSettings.RequestUri; client.Timeout = httpPolicy.Timeout; if (!string.IsNullOrWhiteSpace(httpSettings.UserName) && !string.IsNullOrWhiteSpace(httpSettings.Password)) { var byteArray = Encoding.ASCII.GetBytes($"{httpSettings.UserName}:{httpSettings.Password}"); client.BaseAddress = httpSettings.RequestUri; client.Timeout = httpPolicy.Timeout; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); return(client); } if (!string.IsNullOrWhiteSpace(httpSettings.AuthorizationToken)) { client.BaseAddress = httpSettings.RequestUri; client.Timeout = httpPolicy.Timeout; client.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse(httpSettings.AuthorizationToken); return(client); } return(client); }