最近开发某wap系统,作为原有asp+SQLServer申请注册码系统的补充。wap使用的wml比html严格很多,给调试带来不少麻烦。更有中文编码问题,初次处理,走了不少弯路。
一个提高开发速度的办法是,先使用模拟器来调试程序输出的wml文件,完成大体结构开发。winwap是个不错的选择,用来初步调试还是不错的,不过它和真实手机提交的信息采用的编码不同,最后还是要使用真实手机完成测试。
wap中使用中文其实也比较简单,只要在页面中指定编码方式为gb2312就可以正确显示中文。代码中是这样写的:
<% Response.ContentType = "text/vnd.wap.wml"
Response.CharSet = "gb2312" %><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
wml页面中是这样书写提交信息的,其中的 <input name="Softname" size="8" />
输入框将输入中文信息:
<input name="Softname" size="8" />
<anchor>下一步
<go href="mainmenu.asp?cmd=3" method="get" >
<postfield name="ACCESS_ID" value="<%=Trim(Request("ACCESS_ID"))%>"/>
<postfield name="ACCESS_PWD" value="<%=Trim(Request("ACCESS_PWD"))%>"/>
<postfield name="PHONETYPE_ID" value="$(ed_PHONETYPE_ID)"/>
<postfield name="IMEI" value="<%=Trim(Request("IMEI"))%>"/>
<postfield name="APP_DESC" value="$(Softname)"/>
</go>
</anchor>
但是wap浏览器中提交的中文不是gb2312编码,而是utf8编码的。
从网上找了一个转换函数utf2gb,还比较好用,出处未标原作者姓名,在此对作者表示感谢了!
<%
'用途:將UTF-8編碼漢字轉換為GB2312碼,兼容英文和數字
'版權:雖說是原創,其實也參考了別人的部分算法
'用法:Response.write UTF2GB("%E9%83%BD%E5%B8%82%E6%83%85%E7%B7%A3 %E6%98%9F%E5%BA%A7")
Function UTF2GB(UTFStr)
on error resume next
for Dig=1 to len(UTFStr)
if mid(UTFStr,Dig,1)="%" then
if len(UTFStr) >= Dig+8 then
GBStr=GBStr & ConvChinese(mid(UTFStr,Dig,9))
Dig=Dig+8
else
GBStr=GBStr & mid(UTFStr,Dig,1)
End if
else
GBStr=GBStr & mid(UTFStr,Dig,1)
End if
next
if err.Number <> 0 Then
UTF2GB=""
Else
UTF2GB=GBStr
End IF
End Function
Function ConvChinese(x)
A=split(mid(x,2),"%")
i=0
j=0
For i=0 to ubound(A)
A(i)=c16to2(A(i))
Next
For i=0 to ubound(A)-1
DigS=instr(A(i),"0")
Unicode=""
For j=1 to DigS-1
if j=1 then
A(i)=right(A(i),len(A(i))-DigS)
Unicode=Unicode & A(i)
else
i=i+1
A(i)=right(A(i),len(A(i))-2)
Unicode=Unicode & A(i)
End if
Next
if len(c2to16(Unicode))=4 then
ConvChinese=ConvChinese & chrw(int("&H" & c2to16(Unicode)))
else
ConvChinese=ConvChinese & chr(int("&H" & c2to16(Unicode)))
End if
Next
End Function
Function c2to16(x)
i=1
For i=1 to len(x) step 4
c2to16=c2to16 & hex(c2to10(mid(x,i,4)))
Next
End Function
Function c2to10(x)
c2to10=0
If x="0" then
exit Function
End IF
i=0
For i= 0 to len(x) -1
If mid(x,len(x)-i,1)="1" then c2to10=c2to10+2^(i)
next
End Function
Function c16to2(x)
i=0
For i=1 to len(trim(x))
tempstr= c10to2(cint(int("&h" & mid(x,i,1))))
Do while len(tempstr)<4
tempstr="0" & tempstr
Loop
c16to2=c16to2 & tempstr
Next
End Function
Function c10to2(x)
mysign=sgn(x)
x=abs(x)
DigS=1
Do
if x<2^DigS then
exit do
else
DigS=DigS+1
End if
Loop
tempnum=x
i=0
For i=DigS to 1 step-1
If tempnum>=2^(i-1) then
tempnum=tempnum-2^(i-1)
c10to2=c10to2 & "1"
else
c10to2=c10to2 & "0"
End if
next
if mysign=-1 then
c10to2="-" & c10to2
End IF
End Function
%>
使用了此函数调试过程中,发现7610手机提交的中文编码信息,ASP的request("")对象不能正确截取,导致utf2gb函数报错。当时没想到竟然这里出现问题,浪费很多时间,还有一些gprs费用!最后使用以下函数手动截取Request.Form或者Request.QueryString信息中的值,问题得到解决:
Function getRequestValueByName(RequestStr,strName)
'提取html提交信息
'输入RequestStr,格式如cmd=3&PHONETYPE_ID=2&APP_DESC=%e7%9c%bc%e8%ae%af
'输出strName的值,如strName=“cmd“则输出3
Const vbBinaryCompare = 0 '执行二进制比较
Const vbTextCompare = 1 '执行文本比较
Dim searchBeginPos
searchBeginPos = 1
posBegin = Instr(searchBeginPos,RequestStr,strName,vbTextCompare)
If 0 = Int(posBegin) Then
getRequestValueByName = ""
Exit Function
End IF
posBegin = posBegin + Len(strName)+1
searchBeginPos = posBegin
posEnd = Instr(searchBeginPos,RequestStr,"&", vbTextCompare)
IF Int(posEnd) = 0 Then
posEnd = Len(RequestStr) + 1
End IF
iLen = Int(posEnd-posBegin)
If iLen < 1 Then
getRequestValueByName = ""
Else
getRequestValueByName = Mid(RequestStr,posBegin,posEnd-posBegin)
End IF
End Function
%>
不知诸位有没有碰到过这种情况,7610等提交的中文信息,asp的Reqest(“name“)截取错误的情况。M55等却正常。