我一直在试图弄清楚如何读取httpclient调用的内容,而我似乎无法得到它。我得到的响应状态是200,但是我无法弄清楚如何获得返回的实际Json,这就是我所需要的!
以下是我的代码:
async Task<string> GetResponseString(string text)
{
var httpClient = new HttpClient();
var parameters = new Dictionary<string, string>();
parameters["text"] = text;
Task<HttpResponseMessage> response =
httpClient.PostAsync(BaseUri, new FormUrlEncodedContent(parameters));
return await response.Result.Content.ReadAsStringAsync();
}
我只是从方法中调用它:
Task<string> result = GetResponseString(text);
这就是我得到的
response Id = 89, Status = RanToCompletion, Method = "{null}", Result = "StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:\r\n{\r\n Connection: keep-alive\r\n Date: Mon, 27 Oct 2014 21:56:43 GMT\r\n ETag: \"5a266b16b9dccea99d3e76bf8c1253e0\"\r\n Server: nginx/0.7.65\r\n Content-Length: 125\r\n Content-Type: application/json\r\n}" System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage>
更新:这是我目前根据Nathan的回复
的代码 async Task<string> GetResponseString(string text)
{
var httpClient = new HttpClient();
var parameters = new Dictionary<string, string>();
parameters["text"] = text;
var response = await httpClient.PostAsync(BaseUri, new FormUrlEncodedContent(parameters));
var contents = await response.Content.ReadAsStringAsync();
return contents;
}
我用这种方法称呼它......
string AnalyzeSingle(string text)
{
try
{
Task<string> result = GetResponseString(text);
var model = JsonConvert.DeserializeObject<SentimentJsonModel>(result.Result);
if (Convert.ToInt16(model.pos) == 1)
{
_numRetries = 0;
return "positive";
}
if (Convert.ToInt16(model.neg) == 1)
{
_numRetries = 0;
return "negative";
}
if (Convert.ToInt16(model.mid) == 1)
{
_numRetries = 0;
return "neutral";
}
return "";
}
catch (Exception e)
{
if (_numRetries > 3)
{
LogThis(string.Format("Exception caught [{0}] .... skipping", e.Message));
_numRetries = 0;
return "";
}
_numRetries++;
return AnalyzeSingle(text);
}
}
并且它一直在运行,它击中了界限
var model = JsonConvert.DeserializeObject<SentimentJsonModel>(result.Result);
一次,它继续在另一个断点处停止。
当我暂停执行时,它会说
Id =无法计算表达式,因为当前方法的代码已经过优化。,Status =无法计算表达式,因为当前方法的代码已经过优化。,Method =无法计算表达式,因为当前方法的代码已经过优化。 ,Result =无法计算表达式,因为当前方法的代码已经过优化。
..我继续执行,但它只是永远运行。不确定是什么问题
答案 0 :(得分:130)
你使用await / async的方式充其量只是很差,并且很难让你遵循。您正在将await
与Task'1.Result
混合,这只是令人困惑。但是,看起来您正在查看最终任务结果,而不是内容。
我已经重写了你的函数和函数调用,这应该可以解决你的问题:
async Task<string> GetResponseString(string text)
{
var httpClient = new HttpClient();
var parameters = new Dictionary<string, string>();
parameters["text"] = text;
var response = await httpClient.PostAsync(BaseUri, new FormUrlEncodedContent(parameters));
var contents = await response.Content.ReadAsStringAsync();
return contents;
}
你最后的函数调用:
Task<string> result = GetResponseString(text);
var finalResult = result.Result;
甚至更好:
var finalResult = await GetResponseString(text);
答案 1 :(得分:47)
如果您不想使用async
,可以添加.Result
以强制代码同步执行:
private string GetResponseString(string text)
{
var httpClient = new HttpClient();
var parameters = new Dictionary<string, string>();
parameters["text"] = text;
var response = httpClient.PostAsync(BaseUri, new FormUrlEncodedContent(parameters)).Result;
var contents = response.Content.ReadAsStringAsync().Result;
return contents;
}