/// <summary> /// Create a HttpClient which is modified by the settings specified in the IHttpSettings of the HttpBehaviour. /// If nothing is passed, the GlobalSettings are used /// </summary> /// <param name="uriForConfiguration"> /// If a Uri is supplied, this is used to configure the HttpClient. Currently the /// Uri.UserInfo is used to set the basic authorization. /// </param> /// <returns>HttpClient</returns> public static HttpClient Create(Uri uriForConfiguration = null) { var httpBehaviour = HttpBehaviour.Current; var httpSettings = httpBehaviour.HttpSettings ?? HttpExtensionsGlobals.HttpSettings; var httpClient = new HttpClient(HttpMessageHandlerFactory.Create()) { Timeout = httpSettings.RequestTimeout, MaxResponseContentBufferSize = httpSettings.MaxResponseContentBufferSize }; if (!string.IsNullOrEmpty(httpSettings.DefaultUserAgent)) { httpClient.DefaultRequestHeaders.UserAgent.TryParseAdd(httpSettings.DefaultUserAgent); } // If the uri has username/password, use this to set Basic Authorization if (uriForConfiguration != null) { httpClient.SetBasicAuthorization(uriForConfiguration); } // Copy the expect continue value httpClient.DefaultRequestHeaders.ExpectContinue = httpSettings.Expect100Continue; // Allow the passed OnCreateHttpClient action to modify the HttpClient httpBehaviour.OnHttpClientCreated?.Invoke(httpClient); return httpClient; }
/// <summary> /// Create a HttpClient with default, in the HttpClientExtensions configured, settings /// </summary> /// <param name="suppliedHttpSettings">IHttpSettings instance or null if the global settings need to be used</param> /// <param name="uriForConfiguration">If a Uri is supplied, this is used to configure the HttpClient. Currently the Uri.UserInfo is used to set the basic authorization.</param> /// <returns>HttpClient</returns> public static HttpClient CreateHttpClient(IHttpSettings suppliedHttpSettings = null, Uri uriForConfiguration = null) { var httpSettings = suppliedHttpSettings ?? HttpSettings.GlobalHttpSettings; var client = new HttpClient(HttpMessageHandlerFactory.CreateWebRequestHandler(httpSettings)) { Timeout = httpSettings.RequestTimeout, MaxResponseContentBufferSize = httpSettings.MaxResponseContentBufferSize }; if (!string.IsNullOrEmpty(httpSettings.DefaultUserAgent)) { client.DefaultRequestHeaders.UserAgent.TryParseAdd(httpSettings.DefaultUserAgent); } client.SetBasicAuthorization(uriForConfiguration); return client; }