When we use request. querystring today, we find that all received strings are garbled as long as they contain Chinese characters. After research, I finally found the cause.
ASP. NET uses UTF-8 encoding by default, and we generally use gb2312 encoding. This is the reason why Chinese characters become garbled during request. querystring, which brings a lot of trouble to everyone.
Let's take a look at two parameters: "test. aspx? String = % B7 % E7 % A4 % Ce % Ca % C0 % BD % E7 "and" test. aspx? String = % E9 % A3 % 8e % E3 % 81% AE % E4 % B8 % 96% E7 % 95% 8C ". A rough look, this is to pass two different parameters to the test. ASPX page, but after correct URL anti-encoding, we can find that these two are actually the same parameter: the world of wind! Why are two different strings produced by the same parameter? This is because the first parameter is encoded with gb2312 URL, while the second parameter is encoded with a UTF-8. If we use request. querystring ["string"] in test. aspx to return the value, we will find that the first parameter cannot be taken normally, while the second parameter is normal. This is because ASP. NET does not specify the encoding, the default is the use of UTF-8 encoding, natural URL anti-encoding is also used when the UTF-8 encoding. So, gb2312 URL encoding content, with the UTF-8 URL anti-encoding, it is certainly not normal.
The solution is as follows:
1. The submitted parameter is URL encoded by the UTF-8.
In this case, the normal value can be obtained without any processing. For example, I submitted "test. aspx? String = % E9 % A3 % 8e % E3 % 81% AE % E4 % B8 % 96% E7 % 95% 8C ", the method is as follows:
'Visual basic. net
Dim stringvalue as string
Stringvalue = request. querystring ("string ")
Response. Write (stringvalue)
// Visual C #
String stringvalue;
Stringvalue = request. querystring ["string"];
Response. Write (stringvalue );
2. The submitted parameters are encoded by gb2312 URL.
In this case, values cannot be taken directly. You can use the following method:
'Visual basic. net
'Reference the system. Collections. Specialized and system. Text namespaces.
Dim stringvalue as string
Dim gb2312requests as namevaluecollection
Gb2312requests = httputility. parsequerystring (request. url. query, encoding. getencoding ("gb2312 "))
The string in response. Write (gb2312requests ("string") 'is the key of the parameter you submitted.
// Visual C #
'Reference the system. Collections. Specialized and system. Text namespaces.
String stringvalue;
Namevaluecollection gb2312requests;
Gb2312requests = httputility. parsequerystring (request. url. query, encoding. getencoding ("gb2312 "))
Response. Write (gb2312requests ["string"]); // 'string' indicates the key of the parameter you submitted.
Sometimes, we also want to submit URL encoding strings of different codes, so we can continue to look at them.
3. Submit the utf8 URL encoding parameters.
As we have said before, the system uses UTF-8 encoding automatically when encoding is not specified, so we need to submit utf8 URL encoding parameters can directly use server. urlencode.CodeAs follows:
'Visual basic. net
Dim strbefore as string = "Wind compute world"
Dim strafter as string = ""
Strafter = server. urlencode (strbefore)
Response. Write (stralfter)
// Visual C #
String strbefore = "fengyi world ";
String stralfter = "";
Strafter = server. urlencode (strbefore );
Response. Write (stralfter );
4. Submit the gb2312 URL encoding parameters.
Because the system uses UTF-8 encoding by default, you must use gb2312 for URL encoding. You must specify an encoding. The Code is as follows:
'Visual basic. net
'Reference the system. Text namespace
Dim strbefore as string = "Wind compute world"
Dim strafter as string = ""
Strafter = httputility. urlencode (strbefore, encoding. getencoding ("gb2312 "))
Response. Write (stralfter)
// Visual C #
// Reference the system. Text namespace
String strbefore = "fengyi world ";
String stralfter = "";
Strafter = httputility. urlencode (strbefore, encoding. getencoding ("gb2312 "));
Response. Write (stralfter );
In this way, the encoded characters of gb2312 are obtained after URL encoding.
Note that server. urlencode in ASP is URL encoded using gb2312 encoding.