HttpClient.GetAsync 与网络凭据 [英] HttpClient.GetAsync with network credentials
本文介绍了HttpClient.GetAsync 与网络凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在使用 HttpWebRequest
来获取网站.我想使用等待模式,它没有为 HttpWebRequests
提供.我找到了 HttpClient
类,它似乎是新的 Http worker 类.我正在使用 HttpClient.GetAsync(...)
来查询我的网页.但是我缺少添加 ClientCredentials
的选项,例如 HttpWebRequest.Credentials
.有没有办法给HttpClient
认证信息?
I'm currently using HttpWebRequest
to get a website. I'd like to use the await pattern, which is not given for HttpWebRequests
. I found the class HttpClient
, which seems to be the new Http worker class. I'm using HttpClient.GetAsync(...)
to query my webpage. But I'm missing the option to add ClientCredentials
like HttpWebRequest.Credentials
. Is there any way to give the HttpClient
authentication information?
推荐答案
您可以传递 HttpClientHandler 类 带有HttpClient 构造函数:
using (var handler = new HttpClientHandler { Credentials = ... })
using (var client = new HttpClient(handler))
{
var result = await client.GetAsync(...);
}
这篇关于HttpClient.GetAsync 与网络凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文