C# HttpUtility 支持多种认证方式,包括但不限于以下几种:
示例代码:
string username = "myuser";
string password = "mypassword";
string base64Credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64Credentials);
示例代码(需要使用HttpClientHandler
和HttpClient
):
var handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential("myuser", "mypassword");
handler.UseDigestAuthentication();
using (var httpClient = new HttpClient(handler))
{
// 发送请求...
}
示例代码(需要使用HttpClientHandler
和HttpClient
):
var handler = new HttpClientHandler();
handler.UseWindowsAuthentication();
using (var httpClient = new HttpClient(handler))
{
// 发送请求...
}
示例代码:
string accessToken = "myaccesstoken";
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
需要注意的是,以上示例代码仅供参考,实际使用时需要根据具体情况进行调整。另外,C# HttpUtility 类本身并不直接支持这些认证方式,而是通过 HttpClient
类及其相关类(如 HttpClientHandler
)来实现。