所以我有一个通过 POST 方法接受 XML 的页面。这是一小部分代码:
if (Request.ContentType != "text/xml")
throw new HttpException(500, "Unexpected Content Type");
StreamReader stream = new StreamReader(Request.InputStream);
string x = stream.ReadToEnd(); // added to view content of input stream
XDocument xmlInput = XDocument.Load(stream);
我遇到了错误,所以我将流转换为字符串,只是为了查看是否所有内容都已正确发送。当我查看内容时,它看起来像这样:
%3c%3fxml+version%3d%271.0%27+encoding%3d%27UTF-8%27%3f%3e%0d%0a
所以我想我需要解码流。唯一的问题是我不知道如何在流上使用 HtmlDecode,并仍然将其保留为 StreamReader 对象。
有什么办法吗?
最佳答案
显然,客户端将内容作为 URL 编码的 XML 发送。所以你需要像这样解码内容:
StreamReader stream = new StreamReader(Request.InputStream);
string x = stream.ReadToEnd();
string xml = HttpUtility.UrlDecode(x);
XDocument xmlInput = XDocument.LoadXml(xml);
无论如何,问题可能出在客户端......为什么要这样编码 XML?
关于c# - 解码输入流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7957448/