C#:
使用基本认证发送 HTTP 请求
How to: (如何操作)
简单代码示例:
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
class BasicAuthExample
{
static async Task Main(string[] args)
{
var client = new HttpClient();
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("username:password"));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);
try
{
HttpResponseMessage response = await client.GetAsync("http://example.com/protected");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
catch(HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ",e.Message);
}
}
}
输出样例:
<!DOCTYPE html>
<html>
<body>
<h1>Welcome to the protected area</h1>
</body>
</html>
Deep Dive (深入了解)
基本认证由HTTP协议定义,最早在1996年的RFC 1945中提出。它不是最安全的认证方式——比如,若没有TLS/SSL,凭据可被截获。更安全的替代方法包括OAuth2.0和JWT (JSON Web Tokens)。在C#中,发送带基本认证的HTTP请求,重点在于生成合适的Authorization
头部,该头部含有Base64编码的用户名和密码。还可以用HttpClientHandler
来设置凭据,C# 5.0及以上版本支持async
和await
用于更高效的异步操作。
See Also (另请参阅)
- HTTP基本认证规范: RFC7617
- C#
HttpClient
类: MSDN Documentation - Base64编码: Base64 on Wikipedia
- 关于OAuth 2.0: OAuth 2.0 Authorization Framework
- 关于JWT (JSON Web Tokens): Introduction to JSON Web Tokens