当前位置: 首页>>代码示例>>C#>>正文


C# HttpClient.SetBasicAuthorization方法代码示例

本文整理汇总了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;
		}
开发者ID:dapplo,项目名称:Dapplo.HttpExtensions,代码行数:36,代码来源:HttpClientFactory.cs

示例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;
		}
开发者ID:llenroc,项目名称:Dapplo.HttpExtensions,代码行数:22,代码来源:HttpClientFactory.cs


注:本文中的System.Net.Http.HttpClient.SetBasicAuthorization方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。