About httputility. urlencode, httputility. urldecode, server. urlencode, server. urldecode

Source: Internet
Author: User

Httputility. urlencode method:

Encode the URL string to implement reliable HTTP transmission from the Web server to the client.

Reload list
Converts byte arrays to encoded URL strings for reliable HTTP transmission from the Web server to the client.
[C #] public static string urlencode (byte []);

Encode the URL string to implement reliable HTTP transmission from the Web server to the client.
[C #] public static string urlencode (string );

The URL string is encoded using the specified encoding object to implement reliable HTTP transmission from the Web server to the client.
[C #] public static string urlencode (string, encoding );

From the specified position in the array to the specified number of bytes, the byte array is converted into a URL-encoded string to implement reliable HTTP transmission from the Web server to the client.
[C #] public static string urlencode (byte [], Int, INT );

Httputility. urldecode method:

Converts a string that has been encoded for transmission in a URL into a decoded string.

Reload list
Converts a string that has been encoded for transmission in a URL into a decoded string.
[C #] public static string urldecode (string );

Use the specified decoded object to convert the URL-encoded byte array to a decoded string.
[C #] public static string urldecode (byte [], encoding );

Use the specified encoding object to convert a URL-encoded string to a decoded string.
[C #] public static string urldecode (string, encoding );

Use the specified encoding object to convert the URL encoded byte array to a decoded string starting from the specified position in the array to the specified number of bytes.
[C #] public static string urldecode (byte [], Int, Int, encoding );

Server is an instance of the httpserverutility class and a property of system. Web. UI. Page.
Httpserverutility. urlencode Method:
Encode the string for reliable HTTP transmission from the Web server to the client through the URL.

Reload list
Encode the string and return the encoded string.
[C #] Public String urlencode (string );

The URL encodes the string and sends the result output to the textwriter output stream.
[C #] public void urlencode (string, textwriter );
Example:
String teststring = "This Is A <test string> .";
Stringwriter writer = new stringwriter ();
Server. urlencode (teststring, writer );
String encodedstring = writer. tostring ();

Httpserverutility. urldecode method:
Decodes a string that is encoded for HTTP transmission and sent to the server in the URL.

Reload list
Perform URL Decoding on the string and return the decoded string.
[C #] Public String urldecode (string );

Decodes the HTML string received in the URL and sends the result output to the textwriter output stream.
[C #] public void urldecode (string, textwriter );

 

Notes:
1. httputility. urlencode and httputility. urldecode are static methods, while server. urlencode and server. urldecode are instance methods.
2. Server is an instance of the httpserverutility class and a property of system. Web. UI. Page.
3. the string encoded with httputility. urlencode is different from the string object encoded with server. urlencode:
For example:
String url = "http://search.99read.com/index.aspx? Book_search = All & main_str = omyr ";
Response. Write (httputility. urlencode (URL ));
Response. Write ("<br> ");
Response. Write (server. urlencode (URL ));

The output result is:
HTTP % 3A % 2f % 2fsearch.99read.com % 2findex. aspx % 3fbook_search % 3 Dall % 26main_str % 3d % E5 % a5 % a5 % E8 % BF % B7 % E5 % B0 % 94
HTTP % 3A % 2f % 2fsearch.99read.com % 2findex. aspx % 3fbook_search % 3 Dall % 26main_str % 3d % B0 % C2 % C3 % D4 % B6 % FB

Cause: the encoding method of server. urlencode is based on the localProgramThe configured encoding method, while httputility. urlencode is encoded in. Net UTF-8 format by default.

If you change the program:
String url1 = "http://search.99read.com/index.aspx? Book_search = All & main_str = omyr ";
Response. Write (httputility. urlencode (url1, system. Text. encoding. getencoding ("gb2312 ")));
Response. Write ("<br> ");
Response. Write (server. urlencode (url1 ));

The output result is:
HTTP % 3A % 2f % 2fsearch.99read.com % 2findex. aspx % 3fbook_search % 3 Dall % 26main_str % 3d % B0 % C2 % C3 % D4 % B6 % FB
HTTP % 3A % 2f % 2fsearch.99read.com % 2findex. aspx % 3fbook_search % 3 Dall % 26main_str % 3d % B0 % C2 % C3 % D4 % B6 % FB

4. Sometimes the URLs transmitted by other systems are encoded in other encoding methods.
This section describes a self-written method to obtain the querystring of the specified encoding format.

Public String getnonnullquerystring (string key, encoding)
{
// Reference the system. Collections. Specialized and system. Text namespaces.
String stringvalue;
System. Collections. Specialized. namevaluecollection encodingquerystring;
// This method is added in 2.0.
Encodingquerystring = httputility. parsequerystring (request. url. query, encoding );
// The key in 'is the key of the parameter you submitted
Return encodingquerystring [Key]! = NULL? Encodingquerystring [Key]. Trim ():"";
}

Call:
String url = getnonnullquerystring ("url", encoding. utf8). Trim ();

 

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 my own program. By default, aspx is encoded in UTF-8, in my program, gb2312 must be used as the default encoding ( ). The problem occurs and 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.

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.