Let's take a look at the following two URLs. Are they passing the same parameters ??
AAA. aspx? Tag =. Net % BC % Ca % F5
AAA. aspx? Tag =. Net % E6 % 8A % 80% E6 % 9C % af
It seems to be different, in fact they are on the ". NET Technology" urlencode, but one is gb2312 encoding, one is the Utf-8 code.
The following code gets the effect after encoding:
String tmp1 = system. Web. httputility. urlencode (". NET technology", system. Text. encoding. getencoding ("gb2312 "));
String tmp2 = system. Web. httputility. urlencode (". NET technology", system. Text. encoding. utf8 );
Our actual web page may be called by other programs.
For example, an ASP page on a simplified Chinese operating system must pass a Chinese parameter to an Asp.net page.
By default, on the simplified Chinese operating system, ASP server. urlencode will encode Chinese in gb2312 encoding,
However, by default, the Asp.net page uses the UTF-8 encoding.
In this case, when you use request. querystring ["tag"] to accept the value, you will not be able to accept the Chinese information. What you can see in One-Step debugging is garbled characters.
At this time, although request. querystring ["tag"] is used to accept garbled characters, the URL at this time is not garbled.
The solution is to analyze the parameters in the URL, and then the parameter value according to gb2312 Encoding Anti-decryption, rather than using. Net default Utf-8 Encoding Anti-decryption.
In fact, Microsoft provides similar functions, so we don't have to use regular expressions to analyze URL strings.
The Demo code is as follows:
String q = request. url. query;
System. Collections. Specialized. namevaluecollection NV =
System. Web. httputility. parsequerystring (Q, system. Text. encoding. getencoding ("gb2312 "));
Response. Write (NV ["tag"]);
Let's use Lutz roeder's. Net reflector to view the implementation of the system. Web. httputility. parsequerystring method:
We can see that the code for final URL parameter string analysis is as follows:
The following functions of the system. Web. httpvaluecollection class parse URL parameters:
Here we can see that it is a one-Character Analysis.
Internal void fillfromstring (string S, bool urlencoded, encoding)
{
Int num1 = (s! = NULL )? S. Length: 0;
For (INT num2 = 0; num2 <num1; num2 ++)
{
Int num3 = num2;
Int num4 =-1;
While (num2 <num1)
{
Switch (s [num2])
{
Case '= ':
If (num4 <0)
{
Num4 = num2;
}
Break;
}
Num2 ++;
}
String text1 = NULL;
String text2 = NULL;
If (num4> = 0)
{
Text1 = S. substring (num3, num4-num3 );
Text2 = S. substring (num4 + 1, (num2-num4)-1 );
}
Else
{
Text2 = S. substring (num3, num2-num3 );
}
If (urlencoded)
{
Base. Add (httputility. urldecode (text1, encoding), httputility. urldecode (text2, encoding ));
}
Else
{
Base. Add (text1, text2 );
}
If (num2 = (num1-1) & (s [num2] = '&'))
{
Base. Add (null, String. Empty );
}
}
}
As for the encoding method that the other party passes to itself, it is best to pass it together as a parameter so that we can perform decryption based on the user's parameter.
Http://blog.joycode.com/ghj/archive/2006/04/19/74894.aspx