从HttpResponseMessage获取内容/消息

Cle*_*lem 155 c# windows-8 windows-store-apps

我正在尝试获取HttpResponseMessage的内容.它应该是:{"message":"Action '' does not exist!","success":false}但我不知道如何从HttpResponseMessage中获取它.

HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync("http://****?action=");
txtBlock.Text = Convert.ToString(response); //wrong!
Run Code Online (Sandbox Code Playgroud)

在这种情况下,txtBlock将具有以下值:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Vary: Accept-Encoding
  Keep-Alive: timeout=15, max=100
  Connection: Keep-Alive
  Date: Wed, 10 Apr 2013 20:46:37 GMT
  Server: Apache/2.2.16
  Server: (Debian)
  X-Powered-By: PHP/5.3.3-7+squeeze14
  Content-Length: 55
  Content-Type: text/html
}
Run Code Online (Sandbox Code Playgroud)

rud*_*den 331

我认为最简单的方法就是将最后一行更改为

txtBlock.Text = await response.Content.ReadAsStringAsync(); //right!
Run Code Online (Sandbox Code Playgroud)

这样您就不需要引入任何流读取器,也不需要任何扩展方法.

  • 您还可以使用Response.Content.ReadAsStringAsync().结果而不是使用await (13认同)
  • 请注意:如果响应中有表情符号或其他一些Unicode字符,则ReadAsStringAsync()可以抛出.我不得不使用Streams(就像在接受的答案中一样)来克服这个问题. (8认同)
  • 不确定为什么这不是可接受的答案,特别是因为这使您能够轻松地将内容序列化到您的对象中. (5认同)
  • ReadAsStringAsync不能很好地处理错误恕我直言. (3认同)
  • @Justin不使用异步方法的结果属性,一直等待它。`Result` 属性可能会导致死锁 https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming#async-all-the -方式 (2认同)

Ice*_*ind 61

你需要调用GetResponse().

Stream receiveStream = response.GetResponseStream ();
StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);
txtBlock.Text = readStream.ReadToEnd();
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,但为什么我在这里得到这个错误:"System.Net.Http.HttpResponseMessage"不包含'GetResponseStream'的定义,也没有扩展方法'GetResponseStream'接受类型'System.Net.Http.HttpResponseMessage'的第一个参数可以找到" (32认同)
  • 这个答案完全偏离主题,OP使用的是"HttpClient",而不是"HttpWebRequest"/"HttpWebResponse". (17认同)
  • @Klemzy - 因为你是异步调用它.请尝试使用"Content"属性.看看[这里的例子](http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh781239.aspx).向下滚动到第二步. (13认同)
  • @Klemzy - 请看[这里的例子](http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh781239.aspx).向下滚动到第二步.如果你无法弄清楚,我会编辑我的答案并为你举个例子 (2认同)
  • 问题是关于 HttpCient,您的回复基于过时和过时的 HttpWebRequest。 (2认同)

MCu*_*elo 36

试试这个,你可以创建一个像这样的扩展方法:

    public static string ContentToString(this HttpContent httpContent)
    {
        var readAsStringAsync = httpContent.ReadAsStringAsync();
        return readAsStringAsync.Result;
    }
Run Code Online (Sandbox Code Playgroud)

然后,简单地调用扩展方法:

txtBlock.Text = response.Content.ContentToString();
Run Code Online (Sandbox Code Playgroud)

我希望这能帮助你;-)

  • 如果您的代码无法处理异步编程,请使用“await”而不是“.Result”...或使用同步 HTTP 客户端。但任何现代代码都应该这样做,否则这可能表明您的应用程序做错了什么。 (2认同)

tar*_*fir 8

如果要将其强制转换为特定类型(例如,在测试中),可以使用ReadAsAsync扩展方法:

object yourTypeInstance = await response.Content.ReadAsAsync(typeof(YourType));
Run Code Online (Sandbox Code Playgroud)

或者跟随同步代码:

object yourTypeInstance = response.Content.ReadAsAsync(typeof(YourType)).Result;
Run Code Online (Sandbox Code Playgroud)

更新:还有ReadAsAsync <>的通用选项,它返回特定的类型实例而不是对象声明的实例:

YourType yourTypeInstance = await response.Content.ReadAsAsync<YourType>();
Run Code Online (Sandbox Code Playgroud)

  • object yourTypeInstance = await response.Content.ReadAsAsync(typeof(YourType)); 应该是var yourTypeInstance = await response.Content.ReadAsAsync <YourType>(); (2认同)

sta*_*rsp 6

由 rudivonstaden 的回答

txtBlock.Text = await response.Content.ReadAsStringAsync();
Run Code Online (Sandbox Code Playgroud)

但如果你不想使方法异步,你可以使用

txtBlock.Text = response.Content.ReadAsStringAsync();
txtBlock.Text.Wait();
Run Code Online (Sandbox Code Playgroud)

Wait() 很重要,因为我们正在执行异步操作,我们必须等待任务完成才能继续。

  • 使用 `.Result` 有什么不同吗?`httpContent.ReadAsStringAsync().Result` (3认同)

ben*_*gen 5

我建议的快速答案是:

response.Result.Content.ReadAsStringAsync().Result

  • 不要对任务调用“Result”。您可能会面临锁定应用程序的风险。使用 async/await 代替。 (4认同)