简体   繁体   English

C#HttpRequest和不同的编码

[英]C# HttpRequest and different encodings

Here is my problem. 这是我的问题。 I have a website in ASP.NET / C# which receives some data via GET/POST 我有一个ASP.NET / C#网站,通过GET / POST接收一些数据

This is "user filled" data, but not through a web page, it's a software that contacts my server. 这是“用户填充”数据,但不是通过网页,它是与我的服务器联系的软件。

Problem is, this software is sending data encoded in ISO-8859-1 (so Café would be sent as Caf%e9 ) and the rest of my SW/DB is Unicode 问题是,该软件正在发送以ISO-8859-1编码的数据(因此Café将作为Caf%e9发送),其余的SW / DB是Unicode

Also the data gets completely mangled, making recovery of what has been sent impossible :/ 此外,数据完全被破坏,恢复已发送的内容:/

What would be the best way to deal with this? 处理这个问题的最佳方法是什么?

I tried setting Request.ContentEncoding (before reading), but no avail. 我尝试设置Request.ContentEncoding(在阅读之前),但没有用。

The only thing that worked here was adding the following code to web.config: 这里唯一有用的是将以下代码添加到web.config:

<configuration>
  <system.web>
    <globalization requestEncoding="iso-8859-1"/>
  </system.web>
</configuration>

And then use 然后使用

Request["varName"]

Do not use HttpUtility.UrlDecode or HttpUtility.UrlEncode , those 2 only work on the raw query string. 不要使用HttpUtility.UrlDecodeHttpUtility.UrlEncode ,这些只能用于原始查询字符串。 Request[] already does the decode for you. 请求[]已经为您解码。

Thanks to JamesP for posting the idea himself. 感谢JamesP自己发布这个想法。

%e9 is just é but UrlEncoded. %e9只是é但UrlEncoded。 Server.UrlDecode your request string. Server.UrlDecode您的请求字符串。

Look at 看着

How to: Select an Encoding for ASP.NET Web Page Globalization 如何:为ASP.NET网页全球化选择编码

Short: 短:

In the web.config write 在web.config中写

<configuration>
  <system.web>
    <globalization
      fileEncoding="utf-8"
      requestEncoding="utf-8"
      responseEncoding="utf-8"
      culture="en-US"
      uiCulture="de-DE"
    />
  </system.web>
</configuration>

Remove the encoding entries in the aspx headers. 删除aspx头中的编码条目。

If utf-8 not correct try utf-16 如果utf-8不正确,请尝试使用utf-16

I hope this helps. 我希望这有帮助。

Original author of the question here. 这个问题的原作者。

What helped me was Georg suggestion of setting Web.config variables, I put 帮助我的是Georg建议设置Web.config变量,我把它

requestEncoding="iso-8859-1" requestEncoding = “ISO-8859-1”

And everything works now, thanks! 现在一切正常,谢谢!

If I understand you correctly, you are pulling this information out of an HTTP request. 如果我理解正确,您将从HTTP请求中提取此信息。 I'm going to assume it is the HTTP request body that is in the encoding. 我将假设它是编码中的HTTP请求主体。

You can use System.Text.Encoding.GetEncoding(...) to retrieve an Encoding object for ISO-8859-1. 您可以使用System.Text.Encoding.GetEncoding(...)来检索ISO-8859-1的Encoding对象。 Then call GetDecoder() on that encoding object, and use it to interpret the request body. 然后在该编码对象上调用GetDecoder(),并使用它来解释请求体。 Ideally you'd determine the encoding type you load from Encoding.GetEncoding(...) from header values in the request, so servers with different configurations are supported. 理想情况下,您可以从请求中的标头值确定从Encoding.GetEncoding(...)加载的编码类型,因此支持具有不同配置的服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM