The Server. UrlEncode function in ASP.net is different from the Server. URLEncode function in ASP !!!!

Source: Internet
Author: User

Call a download program today. When the downloaded URL contains Chinese characters, it cannot be downloaded, for example:

Http://www.huachu.com.cn/itbook/booklist.asp? Tsmc = assembly

I will use the Server. UrlEncode function in. net for conversion. However, this still does not work. The reason was not found after the experiment for a long time. Later I suspect that the Server. UrlEncode function in ASP.net is different from the value returned by the Server. URLEncode function in ASP. 1. Experiment. Actually, it is.

Test code:

What is the following code in ASP.net? Response. Write (Server. UrlEncode ("assembly "));? Return Value: % e6 % b1 % 87% e7 % bc % 96

The following code in ASP: Response. Write Server. URLEncode ("assembly ")?? Return Value: % BB % E3 % B1 % E0

Cause of this problem: Server. UrlEncode in ASP.net is handled by UTF-8 encoding by default. In ASP, the encoding method is set locally.

If you use the following encoding in ASP.net: ASP and ASP.net, the results will be the same:

Response. Write (HttpUtility. UrlEncode ("assembly", Encoding. Default ));

Use: Response. Write (HttpUtility. UrlEncode ("assembly", Encoding. UTF8 ));? Response. Write (Server. UrlEncode ("assembly "));?? Returned results.

 

 

 

 

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 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://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
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 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 ();

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.