Encoding and decoding in "ASP."

Source: Internet
Author: User
Tags urlencode

Which one should be used when encoding URLs? What's the difference between the two?
Test:

StringFile="Document (biography) of the paper. doc";
StringServer_urlencode=Server.URLEncode (file);
StringServer_urldecode=Server.urldecode (Server_urlencode);
StringHttputility_urlencode=System.Web.HttpUtility.UrlEncode (file);
StringHttputility_urldecode=System.Web.HttpUtility.UrlDecode (Httputility_urlencode);
Response.Write ("Original data:"+file);
Sfun.writeline ( "server.urlencode:" Span style= "color: #000000;" >+server_urlencode);
Sfun.writeline ( "server.urldecode: "+server_urldecode";
Sfun.writeline ( "httputility.urlencode: "+httputility_urlencode";
Sfun.writeline ( "httputility.urldecode: "+httputility_urldecode";

Output:
Original data: On file (biography). doc
Server.urlencode:%ce%c4%bc%fe%c9%cf%a3%a8%b4%ab%a3%a9%c6%aa.doc
Server.urldecode: Document (biography). 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: Document (biography). doc

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

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

However, when we write the 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 Chinese file names.

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

One solution is to replace "+" with "%20" (if the original "+" is converted to "%2b") after HttpUtility's urlencode, such as:

FileName = httputility.urlencode (filename, Encoding.UTF8);
FileName = filename.replace ("+", "%20");

Do not understand why Microsoft should convert the space to a plus sign instead of "%20". Remember that the urlencoder of the JDK is to convert the space into "%20".
This is also the case with the. Net 2.0 check.

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 as the default encoding
(<globalization requestencoding= "gb2312" responseencoding= "gb2312"/>),

Problem arises, previously no problem of Httputility.urldecode in page.request back value is garbled that's what it says. Httputility.urldecode The URL is encoded by default with UTF8, in which case you just change the Httputility.urldecode to Server.URLEncode.

Source Link: Encoding and decoding in ASP.

Encoding and decoding in "ASP."

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.