Modify:
Public static string escape (string S)
{
Stringbuilder sb = new stringbuilder ();
Byte [] BA = system. Text. encoding. Unicode. getbytes (s );
For (INT I = 0; I <ba. length; I + = 2)
{
If (BA [I + 1] = 0)
{
// Numbers, uppercase and lowercase letters, and "+-*/. _" remain unchanged
If (
(BA [I]> = 48 & Ba [I] <= 57)
| (BA [I]> = 64 & Ba [I] <= 90)
| (BA [I]> = 97 & Ba [I] <= 122)
| (BA [I] = 42 | Ba [I] = 43 | Ba [I] = 45 | Ba [I] = 46 | Ba [I] = 47 | Ba [I] = 95)
) // Remain unchanged
{
SB. append (encoding. Unicode. getstring (BA, I, 2 ));
}
Else // % XX format
{
SB. append ("% ");
SB. append (BA [I]. tostring ("X2 "));
}
}
Else
{
SB. append ("% u ");
SB. append (BA [I + 1]. tostring ("X2 "));
SB. append (BA [I]. tostring ("X2 "));
}
}
Return sb. tostring ();
}
Source URL javascript: Escape encoding and C # usage problems. Onsubmit = "form1.text1. value = escape (form1.text1. Value );"
. CS Processing
String A = Httputility. urldecode (text1.value );
Response. Write ();
// C # implement escape encoding: (used in Ajax) and decode it with Unescape in Aspx.
Private String Escape ( String S)
{
Stringbuilder sb = New Stringbuilder ();
Byte [] Ba = System. Text. encoding. Unicode. getbytes (s );
For ( Int I = 0 ; I < Ba. length; I + = 2 )
{
SB. append ( " % U " );
SB. append (BA [I + 1 ]. Tostring ( " X2 " ));
SB. append (BA [I]. tostring ("X2"));
}
ReturnSB. tostring ();
}
Other methods ---------------------------- encodeuricomponent ()/decodeuricomponent () encodeuri ()/decodeuri () note that the above two JavaScript Functions are mostly encoded as UTF-8. If the page is not encoded as UTF-8, additional processing is required. Asp.net sends data to Javascript when gb2312 is used on the page. encodestring = httputility. urlencode ("The question is in the http://www.gyzs.net/", system. text. encoding. utf8) receives <SCRIPT> document. write (decodeuricomponent ('<% = encodestring %>'); </SCRIPT> The following JS script is available on the javascrui send data to Asp.net page <SCRIPT> Document. Write ("<a href = '? T = "+ encodeuri ('in question is in question http://www.gyzs.net') +" '> click me </a> "); </SCRIPT> If the web. if gb2312 is configured in config, the following processing is required: response. write (httputility. urldecode (server. urlencode (request ["T"]), system. text. encoding. utf8); If the web. if UTF-8 is used in config, you can directly use request ["T"]. In addition, note that when the form is submitted using the POST method, URL encoding is performed on the data in the form. Be sure not to repeat it.