asp. net URL contains the Chinese parameter to cause the confusion code solution

  • 2020-05-09 18:26:15
  • OfStack

Question:
Some time ago, I made a function block similar to a friendly link in the system, and it ran well until one day I added a link address similar to the following: http://www.****.com/ user.aspx? id= water day, big problem:
1, from the IE address bar directly input this address, access is correct;
2, do a static page, including the hyperlink, click to visit the right;
3, is to add the link to this function block, click to visit there received is garbled code.
At the beginning, I was greatly confused by this problem. After google got one, I finally figured out the problem. In fact, as long as the link address is not passed by any code, there will be no problem. But once you've added the block, click again, and it's still "http://www.****.com/ user. aspx? id= water day ", but the actual parameter "water day" passed has already passed through the operation similar to Server.UrlEncode, of course, the default is utf-8 encoding. It is for this reason that if the other system does not decode the corresponding code and directly operates the parameter, the above error will be generated.
Solutions:
There are many solutions available on the Internet, the most common of which are:
1. Made in web.config < globalization requestEncoding="gb2312" responseEncoding="gb2312" / > ", this method is feasible indeed, but I think many people don't want to use this method to solve the problem, and set the request and response of the whole project as gb2312. I think it is not a good method, and it is a little inverted, because it will lead to many other problems.
2. Take the aspx document that you want to include the link and change its charset property to gb2312. This method is acceptable, but it is not the best method for the problem in this article, because this function block can be dynamically added to any page. Do you want to modify all pages? It's not appropriate to think about it.
3. Directly use Server.UrlEncode and Server.Decode to add code and decode. This method is feasible for both sender and receiver in one project.
4, using HttpUtility. UrlEncode (query, System. Text. Encoding. GetEncoding (" GB2312 ")); The parameter values in the link are encoded by gb2312. In this method, the other side does not need to do any decoding operation, and the parameters are received directly without messy codes.
There are only four methods mentioned above, and others are similar to them. After careful analysis, I think that the fourth method is the most suitable to solve the current problem, the Chinese parameters such as the operation in 4. But there is another problem, because this is a function that a user can enter the link address freely, so the first thing to do is to analyze these link urls, parse out the parameters, and then carry out 4 operations on these parameters, and then put together the original URL address.
Fortunately, URL parameter parsing C# is already supported by the class library, so you don't have to write complex regular expressions to match. Here I will not go into details, but 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; 
} 

In this case, I have coded all the parameters gb2312. Anyway, if the parameters are in English or Numbers, they will remain unchanged after these operations.

Related articles: