public CDSWebApiService(ServiceConfig config) { this.config = config; HttpMessageHandler messageHandler = new OAuthMessageHandler( config, new HttpClientHandler() { UseCookies = false } ); httpClient = new HttpClient(messageHandler) { BaseAddress = new Uri(config.Url + $"/api/data/v{config.Version}/") }; httpClient.BaseAddress = new Uri(config.Url + $"/api/data/v{config.Version}/"); httpClient.Timeout = TimeSpan.FromSeconds(config.TimeoutInSeconds); httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0"); httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0"); httpClient.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); if (config.CallerObjectId != Guid.Empty) { httpClient.DefaultRequestHeaders.Add("CallerObjectId", config.CallerObjectId.ToString()); } }
public static HttpClient GetHttpClient(string connectionString, string clientId, string redirectUrl, string version) { string url = GetParameterValueFromConnectionString(connectionString, "Url"); string username = GetParameterValueFromConnectionString(connectionString, "Username"); string domain = GetParameterValueFromConnectionString(connectionString, "Domain"); string password = GetParameterValueFromConnectionString(connectionString, "Password"); string authType = GetParameterValueFromConnectionString(connectionString, "AuthType"); try { HttpMessageHandler messageHandler; switch (authType) { case "Office365": case "IFD": case "OAuth": messageHandler = new OAuthMessageHandler(url, clientId, redirectUrl, username, password, new HttpClientHandler()); break; case "AD": NetworkCredential credentials = new NetworkCredential(username, password, domain); messageHandler = new HttpClientHandler() { Credentials = credentials }; break; default: throw new ArgumentOutOfRangeException("Valid authType values are 'Office365', 'IFD', or 'AD'."); } HttpClient httpClient = new HttpClient(messageHandler) { BaseAddress = new Uri(string.Format("{0}/api/data/{1}/", url, version)), Timeout = new TimeSpan(0, 2, 0) //2 minutes }; return(httpClient); } catch (Exception) { throw; } }