C # Asp.net URL: Pass Chinese Parameters

Source: Internet
Author: User
C # Use System. Web. httputility. urlencode instead of server. urlencode to pass Chinese parameters in Asp.net URL.

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 well written. I encountered the same problem in one of my own programs. By default, aspx is encoded with UTF-8, in my program, gb2312 must be used 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.

 

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.