WAP中文字符传递ASP,PHP解决方案[添asp解决源码]

发布时间:2007年06月04日      浏览次数:2505 次
WAP中,中文字符传递的解决方案


(Add by 2005-03-04)很多朋友问到ASP下面表单提交的问题,现给出源码下载,和实例,希望能给大家参考。

注:主要是表单提交时候,是否采用:e , :n ,和默认这集中形式<a>默认是:e,<anchor>默认是:n

测试地址:下载源码到本机测试
用opera,或M3GATE点击测试

源码下载:点击下载
【不能下载时PM通知我】




制作WAP站点,你可以采用任何后台程序来相结合,ASP、ASP.NET、PHP、JSP等。
就我个人而言,我只试过ASP和PHP下的解决方案,经过测试,给出ASP、PHP下的解决方案。

ASP中的问题参考下图:



上图中所所,如果文件采用UTF-8格式存储,字符编码设置为encoding="UTF-8",则会出现:采用A,GET方式提交的时候,有奇偶字符问题,也就是说提交偶数个字符没有问题,提交奇数个字符最后一个字符就会出现乱码;若采用<postfield>,POST方式提交,则还是需要解码16进制的函数。

如果是采用JavaScript则,接收页面:
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="936"%>
……
<%= decodeURI(Request("name"))%>

【2005年1月27日新添】WAP中文字符传递 ASP解决方案

ASP页面中,只要把Request来的数据采用以下函数URLDecode解码就可以了

<%=URLDecode(Request("name"))%>

<%
Function URLDecode(enStr)
      dim deStr
      dim c,i,v
      deStr=""
      for i=1 to len(enStr)
            c=Mid(enStr,i,1)
            if c="%" then
                  v=eval("&h"+Mid(enStr,i+1,2))
                  if v<128 then
                        deStr=deStr&chr(v)
                        i=i+2
                  else
                        if isvalidhex(mid(enstr,i,3)) then
                              if isvalidhex(mid(enstr,i+3,3)) then
                                    v=eval("&h"+Mid(enStr,i+1,2)+Mid(enStr,i+4,2))
                                    deStr=deStr&chr(v)
                                    i=i+5
                              else
                                    v=eval("&h"+Mid(enStr,i+1,2)+cstr(hex(asc(Mid(enStr,i+3,1)))))
                                    deStr=deStr&chr(v)
                                    i=i+3
                              end if
                        else
                              destr=destr&c
                        end if
                  end if
            else
                  if c="+" then
                        deStr=deStr&" "
                  else
                        deStr=deStr&c
                  end if
            end if
      next
      URLDecode=deStr
end function

function isvalidhex(str)
      isvalidhex=true
      str=ucase(str)
      if len(str)<>3 then isvalidhex=false:exit function
      if left(str,1)<>"%" then isvalidhex=false:exit function
            c=mid(str,2,1)
      if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then isvalidhex=false:exit function
            c=mid(str,3,1)
      if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then isvalidhex=false:exit function
end function

%>
<%=URLDecode("%BC%BC%CA%F5%D7%A8%C0%B8")%>



【WAP中文字符传递 PHP解决方案】
现在给出PHP中的完美解决方案,无论是采用普通ASCII码,还是UTF-8格式存储,提交的中文字符奇偶,都没有问题。

采用普通文件格式ASCII码存储,保存为testgb.php
<?php
header("Content-Type: text/vnd.wap.wml;charset=GB2312");
echo '<?xml version="1.0" encoding="GB2312"?>';
echo '<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">';
?>
<wml>
<card id="main" title="PHP Web">
<p>
GET提交,结果:
<?php echo iconv("utf-8", "gb2312",$_GET["name"]);?><br/>
POST提交,结果:
<?php echo iconv("utf-8", "gb2312",urldecode($_POST["name"])); ?><br/>
<input name="name" emptyok="false" tabindex="2" format="*m"/>
<a href="testgb.php?name=$(name:e)">提交aaa</a><br/>
<anchor>提交anchor
<go href="testgb.php" method="post">
<postfield name="name" value="$(name:e)" />
</go>
</anchor><br/>
</p>
</card>
</wml>


采用UTF-8格式存储,保存为test_utf.php
<?php
header("Content-Type: text/vnd.wap.wml;charset=UTF-8");
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">';
?>
<wml>
<card id="main" title="PHP Web">
<p>
GET提交,结果:
<?php echo $_GET["name"]; ?><br/>
POST提交,结果:
<?php echo urldecode($_POST["name"]); ?><br/>
<input name="name" emptyok="false" tabindex="2" format="*m"/>
<a href="test_utf.php?name=$(name:e)">提交aaa</a><br/>
<anchor>提交anchor
<go href="test_utf.php" method="post">
<postfield name="name" value="$(name:e)" />
</go>
</anchor><br/>
</p>
</card>
</wml>


JSP方面我不会,没有测试。

JSP方面,苛子:测试也有中文奇偶字符问题
public static String decode(String s)
{
StringBuffer stringbuffer = new StringBuffer();
s = s.replace('%','0');
for(int i=1; i<s.length(); i+=3)
{
if(s.charAt(i)=='+')
{
stringbuffer.append(' ');
i++;
continue;
}

try {
stringbuffer.append((char)Integer.parseInt(s.substring(i, i+2), 16));
} catch (Exception e) {
}
}
String s1 = stringbuffer.toString();
try
{
byte abyte0[] = s1.getBytes("ISO-8859-1");
s1 = new String(abyte0, "UTF-8");
}
catch(UnsupportedEncodingException unsupportedencodingexception) {}
return s1;
}



苛子说:这个decode是我修改过了的, Servlet里面那个decode好像有问题,解不了奇数个的中文.
这个decode是我修改过了的, Servlet里面那个decode好像有问题,解不了奇数个的中文.

其他可参考:
www.blueidea.com/bbs/NewsDetail.asp?id=1808167
www.blueidea.com/bbs/newsdetail.asp?id=1499094
文章来源:http://www.blueidea.com/bbs/NewsDetail.asp?GroupNa
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!