C # Use System. Web. httputility. urlencode instead of server. urlencode to pass Chinese parameters in Asp.net URL.

Source: Internet
Author: User
Tags urlencode

Parameters on the latest website include Chinese characters, for example:

Http://www.chinapoesy.com/Tag%b0% AE %c7%e9.html

Server. urlencode has already been used. However, some Chinese characters cannot be found in URL rewriting, so there is no problem in URL rewriting standard regular expressions.

Later, GG found that the server. urlencode encoding of the problem was by default, while system. Web. httputility. urlencode can specify the encoding. After UTF-8 encoding is specified, it will be fine.

System. Web. httputility. urlencode ("Aiqing", encoding. utf8.

Below is the reprinted:

Which one should I use for URL encoding? Are there any differences between these two methods?
Test:

String File = " File (upload document .doc " ;
String Server_urlencode = Server. urlencode (File );
String Server_urldecode = Server. urldecode (server_urlencode );
String Httputility_urlencode = System. Web. httputility. urlencode (File );
String Httputility_urldecode = System. Web. httputility. urldecode (httputility_urlencode );
Response. Write ( " Original data: " + File );
Sfun. writeline ( " Server. urlencode: " + Server_urlencode );
Sfun. writeline ( " Server. urldecode: " + Server_urldecode );
Sfun. writeline ( " Httputility. urlencode: " + Httputility_urlencode );
Sfun. writeline ( " Httputility. urldecode: " + Httputility_urldecode );

Output:
Original data: file (upload document .doc
Server. urlencode: performance%c41_bc1_few.c91_cf1_a3%a8%b4% AB %a3%a9%c6%aa.doc
Server. urldecode: file (upload example .doc
Httputility. urlencode: Invalid
Httputility. urldecode: file (upload example .doc

The difference is that httputility. urlencode () is UTF-8 encoded by default, while server. urlencode () is encoded by default.

ASP. when developing pages, we often use system. web. httputility. urlencode and urldecode PASS Parameters between pages through URLs. the use of encode and decode in pairs is correct.

However, when writing a file download page, we often use the following method to specify the name of the downloaded file:

Response. addheader ( " Content-Disposition " , " Attachment; filename = "
+ Httputility. urlencode (filename, encoding. utf8 ));

The reason for conversion to utf8 is to support Chinese file names.

This is the case because httputility. urlencode converts a space into a plus sign ('+') in encode, and converts the plus sign to a space in Decode. However, the browser cannot understand the plus sign as a space, therefore, if the file name contains spaces, the space of the file downloaded from the browser becomes the plus sign.

One solution is to replace "+" with "% 20" after the urlencode of httputility (if it is "+", it is converted to "% 2B"), for example:

Filename = Httputility. urlencode (filename, encoding. utf8 );
Filename = Filename. Replace ( " + " , " % 20 " );

I don't understand why Microsoft converts spaces to plus signs instead of "% 20". Remember that JDK urlencoder converts spaces to "% 20.
After checking, the same is true in. NET 2.0.

The above is copied from other places and is well written. One of my ownProgramWe also encountered the same problem. The default aspx is UTF-8 encoded. In my program, we must use gb2312 as the default encoding.
(<Globalization requestencoding = "gb2312" responseencoding = "gb2312"/> ),

The question appears. There is no problem with httputility. urldecode in page. the value returned by the request is garbled. This is the httputility mentioned above. by default, urldecode uses utf8 to encode the URL. In this case, you only need to encode httputility. change urldecode to server. urlencode.

Welcome to love you.

Related Article

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.