相关疑难解决方法(0)

如何通过httpclient调用获取内容正文?

我一直在试图弄清楚如何读取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();
}
Run Code Online (Sandbox Code Playgroud)

而我只是从一个方法调用它:

Task<string> result =  GetResponseString(text);
Run Code Online (Sandbox Code Playgroud)

这就是我得到的

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> …
Run Code Online (Sandbox Code Playgroud)

c# dotnet-httpclient

86
推荐指数
2
解决办法
16万
查看次数

标签 统计

c# ×1

dotnet-httpclient ×1