Asp. Net URL encoding decoding (space to a plus sign?) )

Source: Internet
Author: User
Tags urlencode

Which one to use when encoding the URL. What's the difference between using these two?
Test: String file = "on file (pass) article. 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 on (Biography) article. doc
Server.urlencode:%ce%c4%bc%fe%c9%cf%a3%a8%b4%ab%a3%a9%c6%aa.doc
Server.urldecode: File on (Biography) article. doc
Httputility.urlencode:%e6%96%87%e4%bb%b6%e4%b8%8a%ef%bc%88%e4%bc%a0%ef%bc%89%e7%af%87.doc
Httputility.urldecode: File on (Biography) article. doc

The difference is that Httputility.urlencode () encodes the URL by default, and Server.URLEncode () encodes the URL with the default encoding.

When developing pages with ASP.net, we often pass parameters through URLs between pages through System.Web.HttpUtility.UrlEncode and UrlDecode. The use of Encode and Decode in pairs is no problem.

However, when we write 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 converting to UTF8 is to support the Chinese filename.

This time the problem comes, because httputility.urlencode in Encode, the space into a plus sign (' + '), in the Decode when the plus to a space, but the browser is not understand the plus space, so if the file name contains a space, in the browser When you download the file, the space becomes the plus sign.

One solution is to replace "+" with "%20" after HttpUtility UrlEncode (if "+" is converted to "%2b"), such as: FileName = Httputility.urlencode (fi Lename, Encoding.UTF8);
FileName = Filename.replace ("+", "%20");

I do not understand why Microsoft wants to convert a space to a plus sign rather than a "%20". Remember that JDK's urlencoder is to convert spaces into "%20".
Checked, as is the case with. Net 2.0.

The above is copied from somewhere else, well written, my own program also encountered the same problem, the default ASPX is encoded in Utf-8, in my program must use GB2312 for the default encoding
(<globalization requestencoding= "gb2312" responseencoding= "gb2312"/>),

Problem arises, previously no problem httputility.urldecode the value in Page.Request back is garbled that's what it says. Httputility.urldecode default to UTF8 URL encoding, this situation can only be changed Httputility.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.