【发布时间】:2014-03-26 10:49:07
【问题描述】:
我有一个 PCl,我想在其中使用 HttpClient 进行异步调用。我是这样编码的
public static async Task<string> GetRequest(string url)
{
var httpClient = new HttpClient() { MaxResponseContentBufferSize = int.MaxValue };
HttpResponseMessage response = await httpClient.GetAsync(url);
return response.Content.ReadAsStringAsync().Result;
}
但 await 显示错误“无法等待 System.net.http.httpresponsemessage”,如消息。
如果我使用这样的代码,一切都会顺利,但不是异步方式
public static string GetRequest(string url)
{
var httpClient = new HttpClient() { MaxResponseContentBufferSize = int.MaxValue };
HttpResponseMessage response = httpClient.GetAsync(url).Result;
return response.Content.ReadAsStringAsync().Result;
}
我只是希望这个方法以异步方式执行。
这是屏幕截图:
【问题讨论】:
-
“错误”是什么意思:编译错误还是运行时异常?确切的错误信息是什么?
-
请显示准确错误信息。我敢肯定这不仅仅是“无法等待”。
-
我更新了我的问题
-
这在 linqpad 中运行良好
标签: c# asynchronous portable-class-library dotnet-httpclient