Solution to garbled characters caused by URL inclusion of Chinese Parameters

Source: Internet
Author: User
Chinese garbled characters have always been one of the common problems in Web development. For Beginners, various encoding methods may be a bit difficult. This article does not describe these encodings, I explained a small problem I encountered and the solution to the problem, hoping to be useful to everyone.

  Problem:

Some time ago, a functional block similar to a friendly link was created in the system, and it continued to run well until the following link address was added one day: http: // www. ****. COM/user. aspx? Id = shuitian, a major problem occurs:

1. Enter this address directly from the IE address bar. The access is correct;

2. Make a static page, including this hyperlink, and click to access it;

3. Add the link to the function block and click access to receive Garbled text.

At the beginning, this problem was a big problem. After Google gave it a hand, it finally figured out the problem, in fact, as long as the link address is passed without any encoding, there will be no problem. However, after adding this function block, click again. Even though "http: // www. *****. com/user. aspx" is displayed in the IE Address Bar? Id = ", but the parameter" "that is passed in the past has actually passed through a process similar to server. urlencode operations, of course, the default is UTF-8 encoding. For this reason, if the other system does not perform the corresponding decoding, directly operate this parameter, the above error is generated.

  Solution:

There are many solutions on the Internet, the most common of which is the following:

1. In the web. configure <globalization requestencoding = "gb2312" responseencoding = "gb2312"/> in config. This method is indeed feasible, but I think many people do not want to use this method to solve it, the requests and responses of the entire project are set to gb2312. I don't think it is a good solution, but it is a bit difficult. After all, this will lead to many other problems.

2. Modify the charset attribute of the aspx document containing the link to gb2312. This method is acceptable, but it is not the best method for the question in this article, because this function block can be dynamically added to any page. Do you want to modify all pages? Think about it.

3. directly use server. urlencode and server. this method is feasible for both the sender and receiver in a project. The problem is that we need to access others' systems at will, therefore, you can only discard this method.

4. Use httputility. urlencode (query, system. text. encoding. getencoding ("gb2312"); encode the parameter values in the link by gb2312. In this way, the other party does not need to perform any decoding operations. If the parameter is directly received, no garbled characters will occur.

The four methods mentioned above are generally the most, and the others are similar to those mentioned above. After careful analysis, I think the 4th methods are the most suitable for solving the current problem, and the Chinese parameters are carried out as shown in step 4. However, another problem occurs. Because this function allows users to enter the link address freely, the first thing to do is to analyze these link URLs and parse the parameters, then perform 4 operations on these parameters, and then combine them into the original URL address.

Fortunately, the URL Parameter Parsing C # already supports class libraries, so you don't have to write complex regular expressions to match them. I will not elaborate on it here. I will directly post the original code:

Public static string initchineseurl (string chineseurl)
{
Uri url = new uri (chineseurl );
System. Collections. Specialized. namevaluecollection NV = system. Web. httputility. parsequerystring (URL. query, system. Text. encoding. getencoding ("UTF-8 "));
String query = "";
For (INT I = 0; I <NV. Count; I ++)
{
If (query. Trim () = string. Empty)
{
Query = "? "+ NV. Keys [I] +" = "+ httputility. urlencode (NV [I], system. Text. encoding. getencoding (" gb2312 "));
}
Else
{
Query + = "&" + NV. keys [I] + "=" + httputility. urlencode (NV [I], system. text. encoding. getencoding ("gb2312 "));
}
}
String u = chineseurl. Split ('? ') [0] + query;
Return U;
}

Here, all the parameters are encoded in gb2312. If the parameters are English or numbers, they remain unchanged after these operations.
Source: Address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.