Resolving the nightmare of urlpathencode strings

Source: Internet
Author: User

Resolving the nightmare of urlpathencode strings

I personally prefer to use gb2312 as a website, which is always used in Web. config:
<?XML version = "1.0" encoding = "gb2312"?>

<GlobalizationRequestencoding= "Gb2312"Responseencoding= "Gb2312" />

Then the nightmare begins ......

Make a simple page:
< %

Response. Write (Server. urldecode (server. urlpathencode ( " Gender " )) &   " <HR> " )

Response. Write (Httputility. urldecode (server. urlpathencode ( " Gender " ), Encoding. utf8) &   " <HR> " )

% >


Because global settings are set to gb2312

Server. urldecode (string)

It is equivalent

Httputility. urldecode (server. urlpathencode (String), Encoding. getencoding ("gb2312 "))

However, the damn server. urlpathencode (string) is always encoded according to UTF-8, and the corresponding encoding of "gender" is:

% E4 % B8 % A4 % E6 % 80% A7

Try Google decoding: http://www.google.com/search? Q = % E4 % B8 % A4 % E6 % 80% A7

Google decoding is completely correct.

However, when the Page code is gb2312:

Server. urldecode (server. urlpathencode ("Gender"))

Garbled characters are generated when server. urldecode is a UTF-8 encoded string. When manual IE is encoded as UTF-8, we can see "Two Huai.

However, if it is a "gender", it will be displayed correctly.

N types of strings have been tried. In most cases, even Chinese characters can be correctly displayed. For example, if there are "data" and an odd number of Chinese characters, the last word will be cut down. For example, "Database" is changed to "data ".

I used "gender" as an example because "gender" is an even number of Chinese characters but cannot be correctly displayed. The corresponding "gender" is an odd number of Chinese characters, but it can be displayed normally. ("Gender and Gender", "Gender and Gender" also make mistakes .)

The encoding is correct, but decoding sometimes fails.

I wasted countless times trying to convert the UTF-8 string to the gb2312 format.

The result I got is that the urldecode function has a bug, and this bug seems very universal.

ASP does not provide urldecode system functions. The two versions of ASP urldecode have similar problems.

The exaggeration is to look at the following post:

Http://www.matrix.org.cn/forum_view.asp? Forum_id = 1 & view_id = 14847

In JSP: http: // localhost: 8700/acweb/executereport. do? Filename =/batch processing touch version/three parameter settings. VTF; 1

String filename = request. getparameter ("FILENAME ");

Filename = new string (filename. getbytes ("ISO8859-1"), "UTF-8 ");

The result shows that filename is:/batch processing touch version/three parameter settings, missing: Set. VTF; 1

Use server. urlpathencode in Asp.net and return server. urldecode. The error code similar to JSP is decoded!

According to http://dev.csdn.net/article/50/50455.shtm:"MSFamous beforeURLDecoding error"

But why does Sun make the same mistake?

Well, in Asp.net, do not use server. urldecode. Use httputility. urldecode and the corresponding encoding parameter to solve the problem.

However, you must obtain the following: http: // localhost/try. aspx? S = % E4 % B8 % A4 % E6 % 80% A7

It is still a waste of time.

Request. querystring (0)

The obtained string "% E4 % B8 % A4 % E6 % 80% A7" has been automatically incorrectly decoded as UTF-8 "Two Huai.

I am using something similar:

Tw = mid (request. rawurl, 15, request. rawurl. Length-13)

Directly obtain "% E4 % B8 % A4 % E6 % 80% A7", and then use httputility. urldecode to decode it.

Call ...... In Google, there was almost no detailed explanation of this decoding error.ProgramMost of the members are engaged in JSP or WAP development ...... Have you ever encountered such a problem in Asp.net?

Days @ 8:09:47

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.