接收不同编码的post数据

发布于 2024-11-04 07:24:02 字数 365 浏览 0 评论 0原文

我正在尝试与第三方系统集成,并且在文档中提到,当他们通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

恍梦境° 2024-11-11 07:24:02

当您打开流读取器时,您应该传递 HttpRequest 对象上指定的编码。

StreamReader reader = new StreamReader(request.InputStream, request.ContentEncoding);
string xmlData = reader.ReadToEnd();

这应该允许您将请求的原始内容获取到正确的 .NET 字符串中,无论使用什么编码。

When you open the stream reader, you should pass the encoding specified on the HttpRequest object.

StreamReader reader = new StreamReader(request.InputStream, request.ContentEncoding);
string xmlData = reader.ReadToEnd();

This should allow you to get the original contents of the request into a proper .NET string regardless of whatever encoding is used.

傾旎 2024-11-11 07:24:02

始终优先使用 Encoding.UTF8。这将确保在大多数情况下,读取始终以正确的编码标准完成。

StreamReader sr = new StreamReader(Request.InputStream, 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.

StreamReader sr = new StreamReader(Request.InputStream, Encoding.UTF8);

Hope it helps.

等待我真够勒 2024-11-11 07:24:02

您可以在构造时将编码传递给 StreamReader,如下所示:

  StreamReader s = new StreamReader(new FileStream(FILE), Encoding.UTF8);

You can pass an encoding to your StreamReader at construction like so:

  StreamReader s = new StreamReader(new FileStream(FILE), Encoding.UTF8);
冧九 2024-11-11 07:24:02

application/x-www.form-urlencodedHTTP 表单数据不是 XML。

如果您期望当 Content-Typeapplication/x-www.form- 时 Request.InputStream 将是可解析的 XML 字符串,您的代码很可能会失败urlencoded

application/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 the Content-Type is application/x-www.form-urlencoded

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文