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."