Box*_*iom 5 .net basic-authentication dotnet-httpclient
我想HttpClient通过在 URL 中指定用户名和密码来使用HTTP 基本身份验证,如下所示:
var request = new HttpRequestMessage(HttpMethod.Get,
"https://username:password@example.com");
using (var client = new HttpClient()) {
await client.SendAsync(request);
}
Run Code Online (Sandbox Code Playgroud)
但是Authorization,请求中没有发送标头。
有没有办法告诉 HttpClient 支持这一点,还是我必须手动从 URL 获取凭据并自己设置标头?
您需要设置标头而不是通过 URL 传递它
var byteArray = Encoding.ASCII.GetBytes("username:password1234");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4046 次 |
| 最近记录: |