When wml enters the Chinese keyword search in the text box, the value obtained by the server is always garbled. how can this problem be solved? Now we provide a client to encode the key words. the method for server re-parsing should be well-developed. Wml file:
When wml enters the Chinese keyword search in the text box, the value obtained by the server is always garbled. how can this problem be solved? Now we provide a client to encode the key words. the method for server re-parsing should be well-developed.
Wml file:
Input value:
Search
Wmlscript File (hol. wmls ):
Extern function check ()
{
Var x = WMLBrowser. getVar ('txtkey ');
Var xx = URL. escapeString (x );
Var para = 'skey = 'XX;
WMLBrowser. go ('So. aspx? 'Para );
}
Server aspx. cs file content:
Private void Page_Load (object sender, System. EventArgs e)
{
// Place user code here to initialize the page
String url = Request. RawUrl;
String key = QryStrUtil (url, 'skey ');
Key = UnEscapeString (key );
}
There are two other methods:
///
/// Obtain the value of the specified parameter in the original Url
///
/// Original Url
/// Parameters
/// Value
Private string QryStrUtil (string sRawUrl, string sKey)
{
String sValue = string. Empty;
Int index = sRawUrl. IndexOf ('? ');
If (-1! = Index &-1! = SRawUrl. IndexOf ('= '))
{
SRawUrl = sRawUrl. Substring (index 1, sRawUrl. Length-index-1 );
String [] asParaInfo = sRawUrl. Split ('&');
String _ key = string. Empty;
Foreach (string sParaInfo in asParaInfo)
{
If (null = sParaInfo | string. Empty = sParaInfo)
Continue;
Index = sParaInfo. IndexOf ('= ');
If (-1 = index)
Continue;
_ Key = sParaInfo. Substring (0, index );
If (_ key = sKey)
{
SValue = sParaInfo. Substring (index 1, sParaInfo. Length-index-1 );
}
}
}
Return sValue;
}
///
/// Inverse conversion of Chinese character encoding
///
///
///
Private string UnEscapeString (string s)
{
String sub1, sub2, sub3;
While (-1! = S. IndexOf ('% '))
{
Sub1 = s. Substring (0, s. IndexOf ('% '));
Sub2 = s. Substring (sub1.Length, 6 );
Sub3 = s. Remove (0, sub1.Length sub2.Length );
Sub2 = sub2.Remove (0, 1 );
String [] scode = sub2.Split ('% ');
Byte [] bCodes = new byte [scode. Length];
For (int I = 0; I {
Int sh, sl, val;
String code = scode [I];
Sh = Convert. ToInt32 (code. Substring (0, 1), 16 );
Sl = Convert. ToInt32 (code. Substring (1, 1), 16 );
Val = sh * 16 sl;
BCodes [I] = Convert. ToByte (val. ToString (), 10 );
}
Sub2 = Encoding. GetEncoding ('gb2312'). GetString (bCodes );
S = sub1 sub2 sub3;
}
Return s;
}
At this point, this method is complete, and the key obtained in Page_Load is the original phase of the original input string. The EscapeString method is provided here.
///
/// Chinese character encoding conversion (this method is not complete yet and will encode letters and numbers)
///
///
///
Public string EscapeString (string s)
{
String res = string. Empty;
Byte [] bCodes = Encoding. GetEncoding ('gb2312'). GetBytes (s );
ASCIIEncoding ascii = new ASCIIEncoding ();
For (int I = 0; I {
Int val = bCodes [I];
Val = (val <0 )? Val 256: val;
Int sh, sl;
Sh = val/16;
Sl = val;
Res = '% ';