public static async Task <HttpClient> BuildHttpClient(string gridMaster, string apiVersion, string username, SecureString password, TimeSpan?timeout = null) { if (!String.IsNullOrEmpty(gridMaster)) { if (!String.IsNullOrEmpty(apiVersion)) { if (!String.IsNullOrEmpty(username)) { if (password != null) { if (await CommandHelpers.CheckConnection(gridMaster)) { HttpClientHandler Handler = new HttpClientHandler(); Handler.ServerCertificateCustomValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; if (!apiVersion.StartsWith("v")) { apiVersion = $"v{apiVersion}"; } HttpClient Client = new HttpClient(Handler) { BaseAddress = new Uri("https://" + gridMaster + "/wapi/" + apiVersion + "/") }; //The actual default is 100 seconds if (timeout != null) { Client.Timeout = (TimeSpan)timeout; } Client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(username + ":" + SecureStringHelper.ToReadableString(password)))); return(Client); } else { throw new WebException("The grid master could not be contacted."); } } else { throw new ArgumentNullException("password", "The provided password cannot be null."); } } else { throw new ArgumentNullException("username", "The username used to connect to the grid master cannot be null or empty."); } } else { throw new ArgumentNullException("apiVersion", "The api version parameter cannot be null or empty."); } } else { throw new ArgumentNullException("gridMaster", "The value for the grid master cannot be null or empty."); } }
public static async Task <HttpClient> BuildHttpClient(string gridMaster, string apiVersion, TimeSpan?timeout = null) { if (!String.IsNullOrEmpty(gridMaster)) { if (!String.IsNullOrEmpty(apiVersion)) { if (await CommandHelpers.CheckConnection(gridMaster)) { if (InfobloxSessionData.UseSessionData) { if (InfobloxSessionData.Cookie != null && !InfobloxSessionData.Cookie.Expired) { HttpClientHandler Handler = new HttpClientHandler() { CookieContainer = new CookieContainer() }; if (!apiVersion.StartsWith("v")) { apiVersion = $"v{apiVersion}"; } Uri Address = new Uri("https://" + gridMaster + "/wapi/" + apiVersion + "/"); Handler.CookieContainer.Add(Address, InfobloxSessionData.Cookie); Handler.ServerCertificateCustomValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; HttpClient Client = new HttpClient(Handler) { BaseAddress = Address }; //The actual default is 100 seconds if (timeout != null) { Client.Timeout = (TimeSpan)timeout; } return(Client); } else { throw new Exception("There is not a valid cookie to utilize, you must specify a credential."); } } else { throw new Exception("There is not a valid cookie to utilize, you must specify a credential."); } } else { throw new WebException("The grid master could not be contacted."); } } else { throw new ArgumentNullException("apiVersion", "The api version parameter cannot be null or empty."); } } else { throw new ArgumentNullException("gridMaster", "The value for the grid master cannot be null or empty."); } }
public static async Task <HttpClient> BuildHttpClient(string gridMaster, string apiVersion, Cookie cookie, TimeSpan?timeout = null) { if (!String.IsNullOrEmpty(gridMaster)) { if (!String.IsNullOrEmpty(apiVersion)) { if (cookie != null) { if (!cookie.Expired) { if (await CommandHelpers.CheckConnection(gridMaster)) { HttpClientHandler Handler = new HttpClientHandler() { CookieContainer = new CookieContainer() }; if (!apiVersion.StartsWith("v")) { apiVersion = $"v{apiVersion}"; } Uri Address = new Uri("https://" + gridMaster + "/wapi/" + apiVersion + "/"); Handler.CookieContainer.Add(Address, cookie); Handler.ServerCertificateCustomValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; HttpClient Client = new HttpClient(Handler) { BaseAddress = Address }; //The actual default timeout is 100 seconds if (timeout != null) { Client.Timeout = (TimeSpan)timeout; } return(Client); } else { throw new WebException("The grid master could not be contacted."); } } else { throw new ArgumentException($"The provided cookie is expired, it expired on {cookie.Expires.ToUniversalTime().ToString()} UTC"); } } else { throw new ArgumentNullException("cookie", "Cookie cannot be null"); } } else { throw new ArgumentNullException("apiVersion", "The api version parameter cannot be null or empty."); } } else { throw new ArgumentNullException("gridMaster", "The value for the grid master cannot be null or empty."); } }