This morning I was asked a question by a colleague: I said that the parameters received were garbled, let me help solve it.
The platform my colleague is responsible for is built Ext.js framework, and the web.config configuration file is configured with the global "GB2312" encoding:
<globalization requestEncoding="gb2312" responseEncoding="gb2312" fileEncoding="gb2312" culture="zh-CN"/>
When the frontend submits the "Chinese text", the backend receives garbled characters with Request.QueryString["xxx"].
No matter how you decode with System.Web.HttpUtility.UrlDecode("xxx", "encoding type"), it doesn't work.
Principle description: 1: The first thing to determine is that when the client's URL parameters are submitted, Ext.js will encode them before submitting them, and the client's encoding is UTF-8 encoding by default
2: Then why is it garbled when receiving parameters with Request.QueryString["xxx"]?
We reverse the compilation step by step, 2.1: Look at the code for the QueryString property:
2.2: Cut into the FillInQueryStringCollection() method
2.3: Cut: QueryStringEncoding
From the QueryStringEncoding code, the system defaults to the encoding method of the globalization configuration node, and if not, the default is UTF-8 encoding 2.4: Cut into FillFromString(string s, bool urlencoded, Encoding encoding)
From this point we find that all parameter inputs are called once: HttpUtility.UrlDecode(str2, encoding);
When client js submits Chinese to the server in utf-8 encoding, when receiving it with Request.QueryString, it will first decode it once with gb2312 configured by globalization, resulting in garbled characters.
1: The JS encoding method is URT-8
2: The server side has configured the default to GB2312
3: Request.QueryString will call HttpUtility.UrlDecode by default to decode the received parameters with system configuration encoding.
1: The system selects the default encoding in the following order: http request header - >globalization configuration node - default UTF-8
2: When entering the URL directly into Chinese, different browsers may handle it differently, for example: IE does not encode and submits directly, Firefox submits the URL after GB2312 encoding.
3: For unencoded "Chinese characters", after using Request.QueryString internal call HttpUtility.UrlDecode, by gb2312->utf-8,
If the Chinese character is not found, it will be converted to "%ufffd" by default, resulting in irreversible garbled characters.
4: The road to resolution Knowing the principle, there are many ways to solve it: 1: The global unification is UTF-8 encoding, which saves trouble and worry.
2: When GB2312 is globally specified, the url is Chinese, and js must be encoded, such as ext.js framework.
In this way, you can only handle it specially, specifying the encoding and decoding on the server side. Because the default system calls HttpUtility.UrlDecode("xxx", the encoding of the system configuration) once, So you call HttpUtility.UrlEncode("xxx", the encoding configured by the system) again to return to the original urt-8 encoding parameter
Then use HttpUtility.UrlDecode("xxx", utf-8) to decode it. string aaa = request. Request.QueryString["admin"]; Homeowner string a1 = HttpUtility.UrlEncode(aaa, System.Text.Encoding.GetEncoding("GB2312")); string a2 = HttpUtility.UrlDecode(a1,System.Text.Encoding.UTF8);
|