HttpClient.GetAsync与网络凭据 [英] HttpClient.GetAsync with network credentials
问题描述
我目前使用的HttpWebRequest
来得到一个网站。我想使用的等待模式,它不是为 HttpWebRequests
给出。我发现类的HttpClient
,这似乎是新的HTTP工作类。我使用 HttpClient.GetAsync(...)
来查询我的网页。但我错过了选项添加 ClientCredientials
如 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 ClientCredientials
like HttpWebRequest.Credentials
. Is there any way to give the HttpClient
authentication information?
推荐答案
您可以传递的一个实例<一个href=\"http://msdn.microsoft.com/en-us/library/system.net.http.httpclienthandler%28v=vs.110%29.aspx\">HttpClientHandler 类的凭据 HttpClient的构造
You can pass an instance of the HttpClientHandler Class with the credentials to the HttpClient Constructor:
using (var handler = new HttpClientHandler { Credentials = ... })
using (var client = new HttpClient(handler))
{
var result = await client.GetAsync(...);
}
这篇关于HttpClient.GetAsync与网络凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!