接收不同编码的post数据
我正在尝试与第三方系统集成,并且在文档中提到,当他们通过 HttpPost 发送 xml 数据时,他们有时会使用“text/xml charset=\”UTF-8**””作为“ Content-Type”,在其他情况下,他们使用“**application/x-www.form-urlencoded”作为 Content-Type。
解析请求时会有什么不同吗?现在我只是使用以下代码提取帖子数据:
StreamReader reader = new StreamReader(Request.InputStream);
String xmlData = reader.ReadToEnd();
I am trying to integrate with a third-party system and in the documentation is mentions that when they send xml data via HttpPost, they sometimes use "text/xml charset=\"UTF-8**"" for the "Content-Type", and in other cases they use "**application/x-www.form-urlencoded" as the Content-Type.
Would there be any differences in parsing the request? Right now I just pull the post data using the folllowing code:
StreamReader reader = new StreamReader(Request.InputStream);
String xmlData = reader.ReadToEnd();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您打开流读取器时,您应该传递 HttpRequest 对象上指定的编码。
这应该允许您将请求的原始内容获取到正确的 .NET 字符串中,无论使用什么编码。
When you open the stream reader, you should pass the encoding specified on the HttpRequest object.
This should allow you to get the original contents of the request into a proper .NET string regardless of whatever encoding is used.
始终优先使用 Encoding.UTF8。这将确保在大多数情况下,读取始终以正确的编码标准完成。
希望有帮助。
Always give preference to use Encoding.UTF8. This will ensure that, in most cases, the reading is always done in a correct coding standard.
Hope it helps.
您可以在构造时将编码传递给 StreamReader,如下所示:
You can pass an encoding to your StreamReader at construction like so:
application/x-www.form-urlencoded
是 HTTP 表单数据,不是 XML。如果您期望当
Content-Type
为application/x-www.form- 时
Request.InputStream
将是可解析的 XML 字符串,您的代码很可能会失败urlencodedapplication/x-www.form-urlencoded
is HTTP Form Data, not XML.Your code would most likely fail if you expect that
Request.InputStream
will be a parsable XML string when theContent-Type
isapplication/x-www.form-urlencoded