Difference of several HTMLEncode (turn)

Source: Internet
Author: User

First, the encoding in C #

Httputility.htmldecode, Httputility.htmlencode and Server.htmldecode, Server.HTMLEncode and the difference between Httpserverutility.htmldecode andHttpServerUtility.HtmlEncode ?

What is the difference between them and the code that is generally hand-written below?

 Public Static stringHTMLEncodestringstr) {      if(str = =NULL|| str = ="")          return ""; Str. Replace ("<","<"); Str. Replace (">",">"); Str. Replace (" "," "); Str. Replace ("   ","  "); Str. Replace ("/"", """); Str. Replace ("/'","'"); Str. Replace ("/ N","<br/>"); returnstr; }  
[C-sharp]

Answer:

HtmlEncode: is to encode the characters that are not allowed in the HTML source file, usually encode the following characters: "<", ">", "&", "" "," "" and so on;

HtmlDecode: With HTMLEncode exactly the opposite, is decoding the original character;

An easy way to httpserverutility the HtmlEncode (HtmlDecode) of an entity class to access System.Web.HttpUtility.HtmlEncode at run time from an ASP. NET Web Application ( HtmlDecode) method, the HtmlEncode (HtmlDecode) method of the HttpServerUtility entity class is used internally by System.Web.HttpUtility.HtmlEncode (HtmlDecode) method to encode (decode) a character;

Server.HTMLEncode (Server.htmldecode) is actually System.Web.UI.Page class encapsulates the HttpServerUtility entity class HtmlEncode (HtmlDecode) method;

The System.Web.UI.Page class has such a property: Public HttpServerUtility Server{get;}

So it can be considered that:

HtmlEncode method of Server.htmlencode=httpserverutility entity class =httputility.htmlencode;

HtmlDecode method of Server.htmldecode=httpserverutility entity class =httputility.htmldecode;

They are just for the convenience of the call, the encapsulation is done;

Here is a very simple replacement test code, the test results look at the comment:

protected voidPage_Load (Objectsender, EventArgs e) {Testchar ("<");//the less than sign is replaced with <Testchar (">");//the greater than sign is replaced with >Testchar (" ");//English Half-width space is replaced by no substitution; Testchar ("   ");//Chinese full-width space is replaced by no substitution;Testchar ("&");//& replaced by &Testchar ("/'");//single quotation marks are replaced with '; Testchar ("/""); Double quotation marks are replaced with"Testchar ("/ R");//carriage return is replaced by no replacement; Testchar ("/ N");//carriage return is replaced by no replacement; Testchar ("/r/n");//carriage return is replaced by no replacement; }  protected voidTestchar (String str) {Response.Write (Server.HTMLEncode (str)); Response.Write ("----------------------");      Response.Write (Httpuility.htmlencode (str)); Response.Write ("<br/>"); }  
[C-sharp]

So the manual replacement method is still necessary to deal with some HTMLEncode not supported by the replacement.

 Public Static stringHTMLEncodestringstr) {str. Replace ("<","<"); Str. Replace (">",">"); Str. Replace (" "," "); Str. Replace ("   "," "); Str. Replace ("/'","'"); Str. Replace ("/"", """); Str. Replace ("/ N","<br/>"); }  
[C-sharp]

Using reflector to view the implementation of the Httputtility.htmlencode, we can see that it only considers five cases, spaces, and the carriage return is not processed:

 Public Static unsafe voidHtmlEncode (stringvalue, TextWriter output) {      if(Value! =NULL)      {          if(Output = =NULL)          {              Throw NewArgumentNullException ("Output"); }          intnum = Indexofhtmlencodingchars (value,0); if(num = =-1) {output.          Write (value); }          Else          {              intnum2 = value. Length-num; fixed(Char* str = ((Char*value)) {Char* Chptr =str; Char* CHPTR2 =chptr;  while(num-->0) {chPtr2++; Output. Write (chptr2[0]); }                   while(num2-->0) {chPtr2++; CharCH = chptr2[0]; if(Ch <='>')                      {                          Switch(CH) { Case '&': {output. Write ("&"); Continue; }                               Case '/"':  {output. Write ("'"); Continue; }                               Case '"': {output. Write (""");                                 Continue; }                               Case '<': {output. Write ("<"); Continue; }                               Case '>': {output. Write (">"); Continue; }} output.                          Write (CH); Continue; }                      if(Ch >='/x00a0') && (Ch <'ā') {output. Write (" the"); Output. Write ( (int) ch).                          ToString (Numberformatinfo.invariantinfo)); Output. Write (';'); }                      Else{output.                      Write (CH); }                  }              }          }      }  }   
[C-sharp]

Second, JS encoding and decoding

  1. First, Escape/unescape
  2. The Escape:escape method returns a string value (in Unicode format) that contains the contents of the charstring. All spaces, punctuation, accents, and any other non-ASCII characters are replaced with%XX encoding, where xx equals the hexadecimal number that represents the character
  3. Unescape: Returns a decoded string from a string object encoded with the Escape method
  4. Exception characters: @ */+
  5. Second, Encodeuri/decodeuri
  6. encodeURI: The method returns an encoded URI. If the encoding result is passed to decodeURI, the initial string is returned. encodeURI does not encode the following characters: ":", "/", ";" and "?". Please use encodeURIComponent to encode these characters
  7. decodeURI: Returns a decoded string from a string object encoded with the encodeURI method
  8. Exception characters:! @ # $ & * () =:/;? +
  9. Third, encodeuricomponent/decodeuricomponent
  10. The Encodeuricomponent:encodeuricomponent method returns an encoded URI. If the encoding result is passed to decodeURIComponent, the initial string is returned. Because the encodeURIComponent method encodes all characters
  11. decodeURIComponent: Returns a decoded string from a string object encoded with the encodeURIComponent method
  12. Exception characters:! * ( ) ‘

Original:http://blog.csdn.net/wd330260402/article/details/5977989

Difference of several HTMLEncode (turn)

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.