我正在从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的流内容中获取此信息?
答案 0 :(得分:0)
string response;
using(var http = new HttpClient())
{
response = await http.GetStringAsync("http://localhost/api/info");
}
var xml = new XmlDataDocument();
xml.LoadXml(response);
您可以使用GetStringAsync来获取字符串而不是HttpContent对象。您还错过了GetAsync中的等待。
注意:代码尚未经过测试