【发布时间】:2019-12-03 10:23:18
【问题描述】:
我正在调用一个返回Task的方法,在调用方法中我需要读取字符串形式的响应。
这是我放的代码:
static public async Task<HttpResponseMessage> Validate(string baseUri,HttpContent content) {
HttpResponseMessage response = new HttpResponseMessage();
response = await client.PostAsync(baseUri,content);
return response;
}
public string test(){
string postJson = "{Login = \"user\", Password = \"pwd\"}";
HttpContent stringContent = new StringContent(postJson,
UnicodeEncoding.UTF8, "application/json");
HttpResponseMessage result=Validate(Uri,stringContent);
var json = result.Content.ReadAsStringAsync().Result;
}
我期望字符串,但抛出此错误:
Cannot implicitly convert type
'System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>' to
'System.Net.Http.HttpResponseMessage'
【问题讨论】:
-
试试 HttpResponseMessage result=Validate(Uri,stringContent).Result;
-
@auburg:以这种方式阻止可能cause a deadlock。
-
在这种情况下,OP 需要等待对 Validate 的调用
标签: c#