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
- First, Escape/unescape
- 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
- Unescape: Returns a decoded string from a string object encoded with the Escape method
- Exception characters: @ */+
- Second, Encodeuri/decodeuri
- 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
- decodeURI: Returns a decoded string from a string object encoded with the encodeURI method
- Exception characters:! @ # $ & * () =:/;? +
- Third, encodeuricomponent/decodeuricomponent
- 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
- decodeURIComponent: Returns a decoded string from a string object encoded with the encodeURIComponent method
- Exception characters:! * ( ) ‘
Original:http://blog.csdn.net/wd330260402/article/details/5977989
Difference of several HTMLEncode (turn)