Garbled URL with Chinese Parameters

Source: Internet
Author: User
Recently, the last project is actually very simple, that is, splicing a URL. However, the URL parameters require urlencode encoding. For me, this problem is well solved. C # uses httputility. urlencode. asp uses server. urlencode.

Is the problem solved? Problem just started

Because this public steering file is for all substations, substationsCodeThere are two types of files:. NET and ASP. The file encoding formats are also different.

The first big thing started. The file code of the ASP site is gb2312, though. net file format is also gb2312, but because the requestencoding set in webconfig is utf8, no matter how you decode urldecode when receiving Chinese characters, even if you specify to use gb2312 for decoding, garbled characters are returned. At this time, you will change the webconfig file! If it is your own small project, such changes may be nothing, but if it involves many projects, what should you do if you cannot change it ????

let's take a look at the example:
environment:
Asp: File Name: test. ASP file encoding: gb2312
. net: File Name: test. aspx, test. aspx. CS file encoding: gb2312
. net: File Name: Go. aspx, go. aspx. CS file encoding: gb2312
test. ASP code: %< br> STR = server. urlencode ( " Chinese test " )
response. redirect " go. aspx? Name = " & STR
% >

test. aspx. CS code: string STR = httputility. urlencode ( " Chinese test " );
response. redirect ( " go. aspx? Name = " + Str );

Go. aspx. CS code:StringName=Httputility. urldecode (request ["Name"], Encoding. getencoding ("Gb2312"));

Response. Redirect ("Http://www.xxx.com? Name =
"+ Httputility. urlencode (name ));

Actually in go. aspx. in CS, we thought the encoding was set to gb2312, and it should be normal. In fact, it was wrong. I tracked it and decoded it during request ["name, it is decoded according to webconfig, that is, utf8.

OK ,.. net, you can specify the encoding for URL encoding, test. aspx. CS can be changed to: httputility. urlencode ("Chinese test", encoding. getencoding ("UTF-8") such as go. aspx is fully converted after receiving the message.

The server. urlencode of ASP does not have this parameter. What should I do?
Two methods:
1. Save test. ASP as UTF-8 encoding.
2. I just thought of a solution.
As mentioned above, I cannot change the encoding of all the projects involved. If the modification is too large for the website, I can only find another solution.

After a lot of tests, I found that urlencode was transcoded into the % AB % CD format. We all know that urlencode will be returned to you intact during English transcoding. For example, if you upload, the receiver also receives a, % will be converted to % 25, and % 25 will be decoded at urldecode decoding regardless of your encoding to %

well, I have some ideas. Assume that the "Chinese test" is encoded as % AB % Cd % EF % GH STR = server. urlencode ( " Chinese test " ) ' % AB % Cd % EF % GH
str1 = server. urlencode (server. urlencode ( " Chinese test " )) ' % 25ab % 25cd % 25ef % 25gh

Let's change go. aspx. CS.Response. Write (httputility. urldecode (request ["Str"], Encoding. getencoding ("Gb2312"));

Response. Write (httputility. urldecode (request ["Str1"], Encoding. getencoding ("Gb2312"));

View the go. ASPX page:
The first line is garbled.
Article 2: Chinese test

Why? Because the relationship between webconfig and request has been decoded according to utf8 in advance, it is useless to decode the request in the specified format.
The second one, why? Because I encoded the code twice, the request will be decoded to: % AB % Cd % EF % GH in advance.
So I am using urldecode for decoding. OK! Task completed.

Although this question is not a big one, it sometimes makes you feel a headache. For this question, it took me three hours and there was no answer on the Internet, so I wrote it down, I hope to help you. 8cad0260

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.