我正在从具有以下代码的WebApi控制器调用一个现有的get方法(我不能修改它)
[HttpGet]
public HttpResponseMessage Get()
{
XmlDataDocument xmldoc = new XmlDataDocument();
FileStream fs = new FileStream("d:\\document.xml", FileMode.Open, FileAccess.Read);
xmldoc.Load(fs);
string str = xmldoc.DocumentElement.InnerXml;
return new HttpResponseMessage() { Content = new StringContent(str, Encoding.UTF8, "application/xml") };
}我一直在试着像这样读这些信息
HttpClient client = new HttpClient();
HttpResponseMessage response = client.GetAsync("http://localhost/api/info");
HttpContent content = rm.Content;我得到了一个StreamContent,但我现在喜欢做的是读取这个内容,并尝试将其反序列化为一个Xml文档,以便读取节点。
如何从HttpContent的流内容中获取这些信息?
发布于 2015-08-05 01:49:23
string response;
using(var http = new HttpClient())
{
response = await http.GetStringAsync("http://localhost/api/info");
}
var xml = new XmlDataDocument();
xml.LoadXml(response);您可以使用GetStringAsync来获取字符串而不是HttpContent对象。您还在您的中错过了等待。
注意:代码尚未测试。
https://stackoverflow.com/questions/31821790
复制相似问题