Solve | problem | show | page
WML in the text box to enter the Chinese keyword search, the server to get the value is always garbled, how to solve? now provides a client to the keyword coding, service-side reparse method, should be able to solve this problem perfectly.
WML files:
<?xml version= "1.0" encoding= "GB2312"?>
<! DOCTYPE WML public "-//wapforum//dtd WML 1.1//en" "Http://www.wapforum.org/DTD/wml_1.1.xml" >
<WML xml:lang= "ZH-CN" >
<card id= "Card1" title= "card 1" >
<!--submit to another page or service-side sample-->
<do type= "Accept" >
Enter a value:
<input type= "text" name= "Txtkey" maxlength= ""/><br/>
<go href= "Hol.wmls#check ()" method= "POST" >
Search
</go>
</do>
</card>
</wml>
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);
}
Service-side aspx.cs file content:
private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
string url = request.rawurl;
String key = Qrystrutil (URL, "Skey");
Key = Unescapestring (key);
}
There are also two other ways:
<summary>
Gets the value of the set parameter in the original URL
</summary>
<param name= "Srawurl" > Original url</param>
<param name= "SKey" > Parameters </param>
<returns> value </returns>
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;
}
<summary>
Encoding Reverse Conversion
</summary>
<param name= "S" ></param>
<returns></returns>
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<scode. Length; 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 * + sl;
Bcodes[i] = Convert.tobyte (val. ToString (), 10);
}
Sub2 = encoding.getencoding ("gb2312"). GetString (Bcodes);
s = sub1 + Sub2 + sub3;
}
return s;
}
At this point, the method is complete, the key in the Page_Load is the original input string. The Escapestring method is provided here.
<summary>
Encoding conversion (This method is not perfect, will encode letters and numbers, etc.)
</summary>
<param name= "S" ></param>
<returns></returns>
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<bcodes.length; i++)
{
int val = bcodes[i];
val = (val < 0)? Val+256:val;
int SH,SL;
SH = val/16;
SL = val%16;
res = "%";
res = convert.tostring (sh,16);
res = convert.tostring (sl,16);
}
return res;
}
Careful readers will find that if you encode with escapestring and then decode with unescapestring, you may be wrong. So here, you can only encode full-width and Chinese characters. However, there is no need to use these two methods at the same time, this method is added to the principle of the WMLScript escapestring method.
Note: The WMLScript interpretation of this method is. This function computes a new version of the generated s t r i n G. In this version, [R F C 1 7 3 8] The special characters (unsafe, reserved, and nonprinting characters) are replaced by a hexadecimal escape sequence, and the given string can be escaped in such a way that the function can not perform U-l analysis. According to [R F C 1 7 8 3], for special characters in the U n i c o d e character set that are encoded equal to or less than 0 x F F, the escape sequence% x x of the double number format is used.