Httputility. urlencode, urldecode, server. urlencode, and urldecode Encoding

Source: Internet
Author: User

Httputility. urlencode, urldecode, server. urlencode, and urldecode Encoding

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 );

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 );

Note:
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://www.xingzhu.net.cn? Name = Customer Management ";
Response. Write (httputility. urlencode (URL ));
Response. Write ("<br> ");
Response. Write (server. urlencode (URL ));

The output result is:
HTTP % 3A % 2f % 2fwww.xingzhu.net.cn % 3 fname % 3d % E5 % AE % A2 % E6 % 88% B7 % E7 % AE % a1 % E7 % 90% 86
HTTP % 3A % 2f % 2fwww.xingzhu.net.cn % 3 fname % 3d % E5 % AE % A2 % E6 % 88% B7 % E7 % AE % a1 % E7 % 90% 86

Cause: the server. urlencode encoding method is based on the encoding method set by the local program, while httputility. urlencode is encoded in. Net UTF-8 format by default.

If you change the program:

String url1 = "http://www.xingzhu.net.cn/Search.aspx? Type = News & tags = Manufacturing ";
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 % 2fwww.xingzhu.net.cn % 2fsearch. aspx % 3 FTYPE % 3 dnews % 26 tags % 3d % D6 % C6 % D4 % EC % D2 % B5
HTTP % 3A % 2f % 2fwww.xingzhu.net.cn % 2fsearch. aspx % 3 FTYPE % 3 dnews % 26 tags % 3d % E5 % 88% B6 % E9 % 80% A0 % E4 % B8 % 9A

3. 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 ();

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.