Today I encountered a problem, default. aspx? U = % B6 % B9 % C8 % F3 % B8 % FD write Response in the background. write (httputility. urldecode (request. querystring ["U"], system. text. encoding. utf8 ));
Response. Write (httputility. urldecode ("% B6 % B9 % C8 % F3 % B8 % FD", system. Text. encoding. utf8 ));
The result is garbled characters in the front and the Chinese characters are displayed normally. Later I found the following articleArticleTo find the cause. The solution is as follows:
System. Collections. Specialized. namevaluecollection NV = httputility. parsequerystring (request. url. query, encoding. getencoding ("gb2312 "));
Response. Write (NV ["U"]);
By default, you must pay attention to the encoding. For example, if you insert a form into your 163 mailbox or other Chinese Web pages, the default encoding is gb2312.
The reference is as follows,
When request. querystring is used to accept parameters, there are some encoding-related problems. Let's first look at the following requests to see what information is received on the. ASPX page using request. querystring?
Page URL request. querystring ["info"] accepted value case 1 A. aspx? Info = % 25% case 2 A. aspx? Info = % BC % Ca % F5 ????
Situation analysis:
Case 1
A. aspx? Info = % 25 why request. querystring ["info"] The received value is %, instead of % 25, because the request. querystring decodes the URL after receiving the value. Httputility. urldecode ("% 25") is calculated as %.
The above case 1 seems very simple. However, in some special scenarios, we are very depressed.
For example:
You have your own encryptionAlgorithmIn some cases, this encryption algorithm calculates the result with a percent sign, and you need to pass this result to other pages through URL parameters. At this time, you may find that a function cannot be used in some cases. What if the problem is solved?
Solution 1:
Make httputility. urlencode one time before passing the parameter to be passed, remember that the urlencode is encoded according to the UTF-8. In this case, if we want the client to accept % 25, we should pass % 2525.
Remember, you cannot perform urlencode once after each acceptance by the receiver. Instead, urlencode is performed on the sender. If the receiver accepts the request as urlencode, the following situation occurs:
- The sender sends a. aspx? Info = % 25. In this case, if the receiver accepts the URL, everything is correct.
- The sender sends a. aspx? Info = %. At this time, if the receiver accepts the request and then uses urlencode, it will be messy.
In this solution, remember that the number of urlencode and urldecode must match one by one. It cannot be more than once, or less than once. Some people will say that the number of times does not match? For example, in the following case, if you do not pay attention to it, the number of occurrences may not match. This is not what you expect. For example, we have the following features:
A. ASPX page automatically jumps to the page set by the from parameter (request. querystring ["from"] to accept this parameter) based on the input from parameter.
B. The ASPX page is also the same logic. It automatically jumps to the specified page based on the input from parameter (request. querystring ["from"] to accept this parameter.
The C. ASPX page is also the same logic. Based on the input from parameter (this parameter is accepted by request. querystring ["from"]), the page is automatically redirected to the specified page.
In this way, we may write the following link address:
A. aspx? From = B. aspx
A. aspx? From = B. aspx? From = C. aspx
A. aspx? From = B. aspx? From = C. aspx? From = http://blog.joycode.com/ghj/
The following is a little more complicated. I will give the following links, all of which have the parameter. Please tell me which page has accepted the parameter?
Htt: httputility. urlencode ("&") = "% 26" httputility. urlencode ("%") = "% 25"
The address a parameter will be accepted by the page
A. aspx? From = B. aspx? The from = C. aspx & A = 1 a parameter is accepted by the. ASPX page.
A. aspx? From = B. aspx? From = C. aspx % 26a = 1 a parameter is accepted by the B. ASPX page
A. aspx? From = B. aspx? From = C. aspx % 2526a = 1 a parameter is accepted by the C. ASPX page
If you do not want to understand it, think about the following sentence:
Httputility. urldecode is performed every time request. querystring is used to obtain parameters.
Solution 2:
Instead of request. querystring, You can implement a method to obtain query parameters. I will tell you the details after I finish the case 2, because this solution also handles some situations in Case 2.
Case 2
A. aspx? Info = % BC % Ca % F5 the information sent to us is actually the two Chinese characters "technology" after gb2312 encoding. Believe it or not, you can use the following expression to calculate the result: % BC % Ca % F5 httputility. urlencode ("technology", system. text. encoding. getencoding ("gb2312") in the Asp.net system. querystring and so on, are the use of the UTF-8 encoding, if we do not have the problem of coexistence of multiple systems, this problem does not exist at all. However, when you need to interact with other systems, the problem may occur. If you do not know the case 2, you will be troubled by this problem.
For example, the following two addresses are mentioned:
The server. urlencode function in Asp.net is different from the value returned by the server. urlencode function in ASP. How can I transmit Chinese characters between PHP and aspx through URLs?
Solution of Case 2
Use the httputility. parsequerystring function with Encoding
Is similar to the followingCodeTo obtain the query text parameters in the specified format.
System. Collections. Specialized. namevaluecollection NV =
System. Web. httputility. parsequerystring (request. url. query, system. Text. encoding. getencoding ("gb2312 "));
Response. Write (NV ["tag"]);
I want to explain why I know the above solutions because I used reflector to read the implementation code of request. querystring. When viewing the code, we will see an internal method:
Internal methods of the system. Web. httpvaluecollection class:
Internal void fillfromstring (string S, bool urlencoded, encoding)
This internal method implements the function of decrypting query parameters as needed. Unfortunately, in the querystring processing function, when querystring is forcibly specified, httputility. urldecode must be performed once. See the following code:
Public static namevaluecollection parsequerystring (string query, encoding)
{
...
Return new httpvaluecollection (query, false, true, encoding );
}
If we do not want to use solution 1 of Case 1, we need to write a code to parse the query information. We can rewrite it by copying the internal void fillfromstring (string S, bool urlencoded, encoding) method of the system. Web. httpvaluecollection class. But it is depressing: If you use reflector to check the implementation of this function, the code from reflector is wrong. The correct method is as follows: it was completed with the help of Shi fan.
Self-implemented method to parse the required text from the URL query text Query
///
// parse the query text query in the URL into a namevaluecollection.
/// with the help of the Assembly head, Guo hongjun adapted it from System. web. internal methods of the httpvaluecollection class:
// internal void fillfromstring (string S, bool urlencoded, encoding)
///
/// query text to be parsed
// whether URL Decoding is required when parsing text
// when parsing text, decoding Based on the URL encoding
//
Public static namevaluecollection fillfromstring (string query, bool urlencoded, encoding)
{< br> namevaluecollection querystring = new namevaluecollection ();
If (string. isnullorempty (query)
{< br> return querystring;
}
// Make sure that the first character of the query text is not?
If (query. startswith ("? "))
{
Query = query. substring (1, query. Length-1 );
}
Int num1 = (query! = NULL )? Query. Length: 0;
// Traverse each character
For (INT num2 = 0; num2 <num1; num2 ++)
{
Int num3 = num2;
Int num4 =-1;
While (num2 <num1)
{
Switch (Query [num2])
{
Case '= ':
If (num4 <0)
{
Num4 = num2;
}
Break;
Case '&':
Goto breakwhile;
}
Num2 ++;
}
Breakwhile:
string name = NULL;
string val = NULL;
If (num4> = 0)
{< br> name = query. substring (num3, num4-num3);
val = query. substring (num4 + 1, (num2-num4)-1);
}< br> else
{< br> val = query. substring (num3, num2-num3);
}< br> If (urlencoded)
{
querystring. add (httputility. urldecode (name, encoding), httputility. urldecode (Val, encoding);
}< br> else
{< br> querystring. add (name, Val);
}< br> If (num2 = (num1-1) & (Query [num2] = '&'))
{< br> querystring. add (null, String. empty);
}< BR >}
Return querystring;
}
With the above code, we can parse the query parameters as needed, rather than using request. querystring.
Summary
Request. querystring for us: Every time after receiving the parameter, urlencode is done, and urlencode is encoded according to the UTF-8. In most cases, there is no problem, but in some cases, it will bring us trouble. This article will analyze the scenarios that may bring us trouble and solutions.