如何用 HttpClient 替换 WebClient?
How to Replace WebClient with HttpClient?
我的 asp.net mvc web 应用程序中有以下 WebClient:
using (WebClient wc = new WebClient()) // call the Third Party API to get the account id
{
string url = currentURL + "resources/" + ResourceID + "/accounts?AUTHTOKEN=" + pmtoken;
var json = await wc.DownloadStringTaskAsync(url);
}
那么谁能建议我如何将它从 WebClient 更改为 HttpClient?
可以编写如下代码:
string url = 'some url';
// best practice to create one HttpClient per Application and inject it
HttpClient client = new HttpClient();
using (HttpResponseMessage response = client.GetAsync(url).Result)
{
using (HttpContent content = response.Content)
{
var json = content.ReadAsStringAsync().Result;
}
}
更新 1 :
如果你想用 await
关键字替换对 Result
属性 的调用,那么这是可能的,但你必须将此代码放在标记为async
如下
public async Task AsyncMethod()
{
string url = 'some url';
// best practice to create one HttpClient per Application and inject it
HttpClient client = new HttpClient();
using (HttpResponseMessage response = await client.GetAsync(url))
{
using (HttpContent content = response.Content)
{
var json = await content.ReadAsStringAsync();
}
}
}
如果您错过了方法中的 async
关键字,您可能会遇到如下所示的编译时错误
The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task<System.Threading.Tasks.Task>'.
更新 2 :
回答您关于将 'WebClient' 转换为 'WebRequest' 的原始问题,这是您可以使用的代码,...但是 Microsoft(和我)建议您使用第一种方法(通过使用 HttpClient).
string url = currentURL + "resources/" + ResourceID + "/accounts?AUTHTOKEN=" + pmtoken;
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = "GET";
using (WebResponse response = httpWebRequest.GetResponse())
{
HttpWebResponse httpResponse = response as HttpWebResponse;
using (StreamReader reader = new StreamReader(httpResponse.GetResponseStream()))
{
var json = reader.ReadToEnd();
}
}
如果你使用C#8及以上,那么你可以写出非常优雅的代码
public async Task AsyncMethod()
{
string url = 'some url';
// best practice to create one HttpClient per Application and inject it
HttpClient client = new HttpClient();
using HttpResponseMessage response = await client.GetAsync(url);
using HttpContent content = response.Content;
var json = await content.ReadAsStringAsync();
} // dispose will be called here, when you exit of the method, be aware of that
更新 3
要知道为什么 HttpClient
比 WebRequest
和 WebClient
更值得推荐,您可以参考以下链接。
http://www.diogonunes.com/blog/webclient-vs-httpclient-vs-httpwebrequest/
http://blogs.msdn.com/b/henrikn/archive/2012/02/11/httpclient-is-here.aspx
我的 asp.net mvc web 应用程序中有以下 WebClient:
using (WebClient wc = new WebClient()) // call the Third Party API to get the account id
{
string url = currentURL + "resources/" + ResourceID + "/accounts?AUTHTOKEN=" + pmtoken;
var json = await wc.DownloadStringTaskAsync(url);
}
那么谁能建议我如何将它从 WebClient 更改为 HttpClient?
可以编写如下代码:
string url = 'some url';
// best practice to create one HttpClient per Application and inject it
HttpClient client = new HttpClient();
using (HttpResponseMessage response = client.GetAsync(url).Result)
{
using (HttpContent content = response.Content)
{
var json = content.ReadAsStringAsync().Result;
}
}
更新 1 :
如果你想用 await
关键字替换对 Result
属性 的调用,那么这是可能的,但你必须将此代码放在标记为async
如下
public async Task AsyncMethod()
{
string url = 'some url';
// best practice to create one HttpClient per Application and inject it
HttpClient client = new HttpClient();
using (HttpResponseMessage response = await client.GetAsync(url))
{
using (HttpContent content = response.Content)
{
var json = await content.ReadAsStringAsync();
}
}
}
如果您错过了方法中的 async
关键字,您可能会遇到如下所示的编译时错误
The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task<System.Threading.Tasks.Task>'.
更新 2 :
回答您关于将 'WebClient' 转换为 'WebRequest' 的原始问题,这是您可以使用的代码,...但是 Microsoft(和我)建议您使用第一种方法(通过使用 HttpClient).
string url = currentURL + "resources/" + ResourceID + "/accounts?AUTHTOKEN=" + pmtoken;
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = "GET";
using (WebResponse response = httpWebRequest.GetResponse())
{
HttpWebResponse httpResponse = response as HttpWebResponse;
using (StreamReader reader = new StreamReader(httpResponse.GetResponseStream()))
{
var json = reader.ReadToEnd();
}
}
如果你使用C#8及以上,那么你可以写出非常优雅的代码
public async Task AsyncMethod()
{
string url = 'some url';
// best practice to create one HttpClient per Application and inject it
HttpClient client = new HttpClient();
using HttpResponseMessage response = await client.GetAsync(url);
using HttpContent content = response.Content;
var json = await content.ReadAsStringAsync();
} // dispose will be called here, when you exit of the method, be aware of that
更新 3
要知道为什么 HttpClient
比 WebRequest
和 WebClient
更值得推荐,您可以参考以下链接。
http://www.diogonunes.com/blog/webclient-vs-httpclient-vs-httpwebrequest/
http://blogs.msdn.com/b/henrikn/archive/2012/02/11/httpclient-is-here.aspx