本文整理汇总了C#中System.Net.Http.HttpClient.SetBasicAuthorization方法的典型用法代码示例。如果您正苦于以下问题:C# HttpClient.SetBasicAuthorization方法的具体用法?C# HttpClient.SetBasicAuthorization怎么用?C# HttpClient.SetBasicAuthorization使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpClient
的用法示例。
在下文中一共展示了HttpClient.SetBasicAuthorization方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Create
/// <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;
}
示例2: CreateHttpClient
/// <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;
}