wml在文本框中輸入中文關鍵字搜索時,服務端獲取到的值總是亂碼,該如何解決?現提供一個客戶端對關鍵字編碼,服務端再解析的方法,應該可以完美的解決這一問題。
wml文件:
<?XML version="1.0" encoding="GB2312"?>
Wmlscript文件(hol.wmls):
extern function check()
服務端ASPx.cs文件內容:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
string url = Request.RawUrl;
string key = QryStrUtil( url, "skey");
key = UnEscapeString (key);
}
另外還有兩個方法:
/// <summary>
/// 獲取原始Url中的制定參數的值
/// </summary>
/// <param name="sRawUrl">原始Url</param>
/// <param name="sKey">參數</param>
/// <returns>值</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>
/// 漢字編碼反轉換
/// </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 * 16 + sl;
bCodes[i] = Convert.ToByte(val.ToString(),10);
}
sub2 = Encoding.GetEncoding("gb2312").GetString(bCodes);
s = sub1 + sub2 + sub3;
}
return s;
}
至此,該方法以全部完畢,Page_Load中得到的key就是原輸入字符串的原形。在這裡提供EscapeString方法。
/// <summary>
/// 漢字編碼轉換(該方法還不完美,會對字母及數字等編碼)
/// </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;
}