当前位置:编程学习 > wap >>

关于手机POST发送中文字符的测试

答案:

本文章源地址:http://www.itican.net/lmy0083/?cat=3

最近搞一个WAP项目,发现有些手机用POST方式提交中文字符的编码有些奇怪,我的环境是GB18030,支持GBK/GB2312。我开发测试Opera7.60。开发语言JSP/JAVA。

据我所知,JAVA默认传输的字符集是8859_1(单字节字符集),手机上大部分是UTF-8,少部分是GB2312;
关于这些字符集请参考http://www.itican.net/lmy0083/?page_id=88

我主要拿”人”这个汉字测试,

GBK/GB2312(Hex) C8 CB
UTF-8(Hex) E4 BA BA
Unicode 人;

我开发测试以Opera7.60 这个浏览器,比较方便,当然,开发都是按这个浏览器支持的开发的,比如我把opera的字符集设为UTF-8,所有POST的中文字符就会以8859_1 默认的字符集发送(这是单字节字符集,不可能包含中文字符的,中文字符都是双字节,UTF-8为3字节)但是奇怪的是,接收必须
String sPost = new String(request.getParameter(”input_name”).getBytes(”8859_1″),”UTF-8″);
否则不能满足我应用,我不能直接拿
request.getParameter(”input_name”)
来用,当然也不能写成
String sPost = new String(request.getParameter(”input_name”).getBytes(”8859_1″),”其他字符集”);

这样开发,一些手机没问题,可以支持,比如Nokia的6681,但是实际测试发现,有些手机比如N800,Nokia6670,输入中文以这样的方式接收,将会接收到乱码;

于是我拿一些浏览器/模拟器/手机做测试,来看我到底接收到的是那种编码;

测试程序(接收部分,发送部分任意写个input,name为”input_name”):

  1. String sPost= request.getParameter("input_name");
  2. if(sPost== null || sPost.length()==0){sPost= "0";}
  3. String GB2312_TO_UTF_8 = new String(sPost.getBytes("GB2312"),"UTF-8");
  4. String GB2312_TO_GBK = new String(sPost.getBytes("GB2312"),"GBK");
  5. String GB2312_TO_8859_1 = new String(sPost.getBytes("GB2312"),"8859_1");
  6. String GBK_TO_UTF_8 = new String(sPost.getBytes("GBK"),"UTF-8");
  7. String GBK_TO_GB2312 = new String(sPost.getBytes("GBK"),"GB2312");
  8. String GBK_TO_8859_1 = new String(sPost.getBytes("GBK"),"8859_1");
  9. String ISO_8859_1_TO_UTF_8 = new String(sPost.getBytes("8859_1"),"UTF-8");
  10. String ISO_8859_1_TO_GB2312 = new String(sPost.getBytes("8859_1"),"GB2312");
  11. String ISO_8859_1_TO_GBK = new String(sPost.getBytes("8859_1"),"GBK");
  12. String UTF_8_TO_8859_1 = new String(sPost.getBytes("UTF-8"),"8859_1");
  13. String UTF_8_TO_GB2312 = new String(sPost.getBytes("UTF-8"),"GB2312");
  14. String UTF_8_TO_GBK = new String(sPost.getBytes("UTF-8"),"GBK");
  15. System.out.println("<WAP TEST>*********************************************************");
  16. System.out.println("<WAP TEST>ren");
  17. System.out.println("<WAP TEST>          UTF_8  |" + "E4BABA|" + java.net.URLEncoder.encode(sPost,"UTF-8"));
  18. System.out.println("<WAP TEST>          GBK    |" + "C8CB  |" + java.net.URLEncoder.encode(sPost,"GBK"));
  19. System.out.println("<WAP TEST>          GB2312 |" + "C8CB  |" + java.net.URLEncoder.encode(sPost,"GB2312"));
  20. System.out.println("<WAP TEST>          8859_1 |" + "      |" + java.net.URLEncoder.encode(sPost,"8859_1"));
  21. System.out.println("<WAP TEST>GB2312 TO UTF_8  |" + "E4BABA|" + java.net.URLEncoder.encode(GB2312_TO_UTF_8,"UTF-8"));
  22. System.out.println("<WAP TEST>GB2312 TO GBK    |" + "C8CB  |" + java.net.URLEncoder.encode(GB2312_TO_GBK,"GBK"));
  23. System.out.println("<WAP TEST>GB2312 TO 8859_1 |" + "C8CB  |" + java.net.URLEncoder.encode(GB2312_TO_8859_1,"8859_1"));
  24. System.out.println("<WAP TEST>GBK    TO UTF_8  |" + "E4BABA|" + java.net.URLEncoder.encode(GBK_TO_UTF_8,"UTF-8"));
  25. System.out.println("<WAP TEST>GBK    TO GB2312 |" + "C8CB  |" + java.net.URLEncoder.encode(GBK_TO_GB2312,"GB2312"));
  26. System.out.println("<WAP TEST>GBK    TO 8859_1 |" + "C8CB  |" + java.net.URLEncoder.encode(GBK_TO_8859_1,"8859_1"));
  27. System.out.println("<WAP TEST>8859_1 TO UTF_8  |" + "E4BABA|" + java.net.URLEncoder.encode(ISO_8859_1_TO_UTF_8,"UTF-8"));
  28. System.out.println("<WAP TEST>8859_1 TO GB2312 |" + "C8CB  |" + java.net.URLEncoder.encode(ISO_8859_1_TO_GB2312,"GB2312"));
  29. System.out.println("<WAP TEST>8859_1 TO GBK    |" + "C8CB  |" + java.net.URLEncoder.encode(ISO_8859_1_TO_GBK,"GBK"));
  30. System.out.println("<WAP TEST>UTF_8  TO 8859_1 |" + "E4BABA|" + java.net.URLEncoder.encode(UTF_8_TO_8859_1,"8859_1"));
  31. System.out.println("<WAP TEST>UTF_8  TO GB2312 |" + "C8CB  |" + java.net.URLEncoder.encode(UTF_8_TO_GB2312,"GB2312"));
  32. System.out.println("<WAP TEST>UTF_8  TO GBK    |" + "C8CB  |" + java.net.URLEncoder.encode(UTF_8_TO_GBK,"GBK"));

测试结果如下:

N800 Openwave V6.1

  1. <WAP TEST>*********************************************************
  2. <WAP TEST>ren
  3. <WAP TEST>          UTF_8  |E4BABA|%E4%BA%BA
  4. <WAP TEST>          GBK    |C8CB  |%C8%CB
  5. <WAP TEST>          GB2312 |C8CB  |%C8%CB
  6. <WAP TEST>          8859_1 |      |%3F
  7. <WAP TEST>GB2312 TO UTF_8  |E4BABA|%EF%BF%BD%EF%BF%BD
  8. <WAP TEST>GB2312 TO GBK    |C8CB  |%C8%CB
  9. <WAP TEST>GB2312 TO 8859_1 |C8CB  |%C8%CB
  10. <WAP TEST>GBK    TO UTF_8  |E4BABA|%EF%BF%BD%EF%BF%BD
  11. <WAP TEST>GBK    TO GB2312 |C8CB  |%C8%CB
  12. <WAP TEST>GBK    TO 8859_1 |C8CB  |%C8%CB
  13. <WAP TEST>8859_1 TO UTF_8  |E4BABA|%3F
  14. <WAP TEST>8859_1 TO GB2312 |C8CB  |%3F
  15. <WAP TEST>8859_1 TO GBK    |C8CB  |%3F
  16. <WAP TEST>UTF_8  TO 8859_1 |E4BABA|%E4%BA%BA
  17. <WAP TEST>UTF_8  TO GB2312 |C8CB  |%E4%BA%3F
  18. <WAP TEST>UTF_8  TO GBK    |C8CB  |%E4%BA%3F

Opera UTF-8

  1. <WAP TEST>*********************************************************
  2. <WAP TEST>ren
  3. <WAP TEST>          UTF_8  |E4BABA|%C3%A4%C2%BA%C2%BA
  4. <WAP TEST>          GBK    |C8CB  |%3F%3F%3F
  5. <WAP TEST>          GB2312 |C8CB  |%3F%3F%3F
  6. <WAP TEST>          8859_1 |      |%E4%BA%BA
  7. <WAP TEST>GB2312 TO UTF_8  |E4BABA|%3F%3F%3F
  8. <WAP TEST>GB2312 TO GBK    |C8CB  |%3F%3F%3F
  9. <WAP TEST>GB2312 TO 8859_1 |C8CB  |%3F%3F%3F
  10. <WAP TEST>GBK    TO UTF_8  |E4BABA|%3F%3F%3F
  11. <WAP TEST>GBK    TO GB2312 |C8CB  |%3F%3F%3F
  12. <WAP TEST>GBK    TO 8859_1 |C8CB  |%3F%3F%3F
  13. <WAP TEST>8859_1 TO UTF_8  |E4BABA|%E4%BA%BA
  14. <WAP TEST>8859_1 TO GB2312 |C8CB  |%E4%BA%3F
  15. <WAP TEST>8859_1 TO GBK    |C8CB  |%E4%BA%3F
  16. <WAP TEST>UTF_8  TO 8859_1 |E4BABA|%C3%A4%C2%BA%C2%BA
  17. <WAP TEST>UTF_8  TO GB2312 |C8CB  |%C3%A4%C2%BA%C2%BA
  18. <WAP TEST>UTF_8  TO GBK    |C8CB  |%C3%A4%C2%BA%C2%BA

[page_break]

Opera GBK/GB2312

  1. <WAP TEST>*********************************************************
  2. <WAP TEST>ren
  3. <WAP TEST>          UTF_8  |E4BABA|%C3%88%C3%8B
  4. <WAP TEST>          GBK    |C8CB  |%3F%3F
  5. <WAP TEST>          GB2312 |C8CB  |%3F%3F
  6. <WAP TEST>          8859_1 |      |%C8%CB
  7. <WAP TEST>GB2312 TO UTF_8  |E4BABA|%3F%3F
  8. <WAP TEST>GB2312 TO GBK    |C8CB  |%3F%3F
  9. <WAP TEST>GB2312 TO 8859_1 |C8CB  |%3F%3F
  10. <WAP TEST>GBK    TO UTF_8  |E4BABA|%3F%3F
  11. <WAP TEST>GBK    TO GB2312 |C8CB  |%3F%3F
  12. <WAP TEST>GBK    TO 8859_1 |C8CB  |%3F%3F
  13. <WAP TEST>8859_1 TO UTF_8  |E4BABA|%EF%BF%BD%EF%BF%BD
  14. <WAP TEST>8859_1 TO GB2312 |C8CB  |%C8%CB
  15. <WAP TEST>8859_1 TO GBK    |C8CB  |%C8%CB
  16. <WAP TEST>UTF_8  TO 8859_1 |E4BABA|%C3%88%C3%8B
  17. <WAP TEST>UTF_8  TO GB2312 |C8CB  |%3F%3F%3F%3F
  18. <WAP TEST>UTF_8  TO GBK    |C8CB  |%C3%88%C3%8B

Nokia 6681

  1. <WAP TEST>*********************************************************
  2. <WAP TEST>ren
  3. <WAP TEST>          UTF_8  |E4BABA|%C3%A4%C2%BA%C2%BA
  4. <WAP TEST>          GBK    |C8CB  |%3F%3F%3F
  5. <WAP TEST>          GB2312 |C8CB  |%3F%3F%3F
  6. <WAP TEST>          8859_1 |      |%E4%BA%BA
  7. <WAP TEST>GB2312 TO UTF_8  |E4BABA|%3F%3F%3F
  8. <WAP TEST>GB2312 TO GBK    |C8CB  |%3F%3F%3F
  9. <WAP TEST>GB2312 TO 8859_1 |C8CB  |%3F%3F%3F
  10. <WAP TEST>GBK    TO UTF_8  |E4BABA|%3F%3F%3F
  11. <WAP TEST>GBK    TO GB2312 |C8CB  |%3F%3F%3F
  12. <WAP TEST>GBK    TO 8859_1 |C8CB  |%3F%3F%3F
  13. <WAP TEST>8859_1 TO UTF_8  |E4BABA|%E4%BA%BA
  14. <WAP TEST>8859_1 TO GB2312 |C8CB  |%E4%BA%3F
  15. <WAP TEST>8859_1 TO GBK    |C8CB  |%E4%BA%3F
  16. <WAP TEST>UTF_8  TO 8859_1 |E4BABA|%C3%A4%C2%BA%C2%BA
  17. <WAP TEST>UTF_8  TO GB2312 |C8CB  |%C3%A4%C2%BA%C2%BA
  18. <WAP TEST>UTF_8  TO GBK    |C8CB  |%C3%A4%C2%BA%C2%BA

Openwave V7 Simulator

  1. <WAP TEST>*********************************************************
  2. <WAP TEST>ren
  3. <WAP TEST>          UTF_8  |E4BABA|%E4%BA%BA
  4. <WAP TEST>          GBK    |C8CB  |%C8%CB
  5. <WAP TEST>          GB2312 |C8CB  |%C8%CB
  6. <WAP TEST>          8859_1 |      |%3F
  7. <WAP TEST>GB2312 TO UTF_8  |E4BABA|%EF%BF%BD%EF%BF%BD
  8. <WAP TEST>GB2312 TO GBK    |C8CB  |%C8%CB
  9. <WAP TEST>GB2312 TO 8859_1 |C8CB  |%C8%CB
  10. <WAP TEST>GBK    TO UTF_8  |E4BABA|%EF%BF%BD%EF%BF%BD
  11. <WAP TEST>GBK    TO GB2312 |C8CB  |%C8%CB
  12. <WAP TEST>GBK    TO 8859_1 |C8CB  |%C8%CB
  13. <WAP TEST>8859_1 TO UTF_8  |E4BABA|%3F
  14. <WAP TEST>8859_1 TO GB2312 |C8CB  |%3F
  15. <WAP TEST>8859_1 TO GBK    |C8CB  |%3F
  16. <WAP TEST>UTF_8  TO 8859_1 |E4BABA|%E4%BA%BA
  17. <WAP TEST>UTF_8  TO GB2312 |C8CB  |%E4%BA%3F
  18. <WAP TEST>UTF_8  TO GBK    |C8CB  |%E4%BA%3F

Openwave V6.2.2 GB2312

  1. <WAP TEST>*********************************************************
  2. <WAP TEST>ren
  3. <WAP TEST>          UTF_8  |E4BABA|%C3%88%C3%8B
  4. <WAP TEST>          GBK    |C8CB  |%3F%3F
  5. <WAP TEST>          GB2312 |C8CB  |%3F%3F
  6. <WAP TEST>          8859_1 |      |%C8%CB
  7. <WAP TEST>GB2312 TO UTF_8  |E4BABA|%3F%3F
  8. <WAP TEST>GB2312 TO GBK    |C8CB  |%3F%3F
  9. <WAP TEST>GB2312 TO 8859_1 |C8CB  |%3F%3F
  10. <WAP TEST>GBK    TO UTF_8  |E4BABA|%3F%3F
  11. <WAP TEST>GBK    TO GB2312 |C8CB  |%3F%3F
  12. <WAP TEST>GBK    TO 8859_1 |C8CB  |%3F%3F
  13. <WAP TEST>8859_1 TO UTF_8  |E4BABA|%EF%BF%BD%EF%BF%BD
  14. <WAP TEST>8859_1 TO GB2312 |C8CB  |%C8%CB
  15. <WAP TEST>8859_1 TO GBK    |C8CB  |%C8%CB
  16. <WAP TEST>UTF_8  TO 8859_1 |E4BABA|%C3%88%C3%8B
  17. <WAP TEST>UTF_8  TO GB2312 |C8CB  |%3F%3F%3F%3F
  18. <WAP TEST>UTF_8  TO GBK    |C8CB  |%C3%88%C3%8B

由测试结果可以看出:
1 N800 (Openwave V6.1)接收的Post中文字符 可以转换为UTF-8,GB2312/GBK都可以,都正确。
2 Opera UTF-8 只能采取8859_1转换UTF-8形式,某些手机是这样,可以支持。
3 Opera GBK/GB2312 只能采取8859_1转换GB2312/GBK形式,某些手机是这样,可以支持。
4 Nokia 6681 该手机就是和Opera UTF-8 测试结果关键部分相同,实际测试也是这样;
5 Openwave V7 Simulator测试结果同N800 (Openwave V6.1);
6 Openwave V6.2.2 GB2312 测试结果同Opera GBK/GB2312 ;

然而我是按Opera UTF-8 模式开发的(当初想的比较少),所以我开发的程序只适合2,4;起码N800就不支持;

解决方法:

  1. if(request.getMethod().equals("POST")){
  2.        //具体代码不一一列举
  3.        //判断是POST过来的中文字符
  4.        //具体方法如下,先认为该字符的编码按8859_1   encode,然后匹配该字符的hex,如果有 %3F(问号)则认为该字符是乱码,则将该字符转换为
  5.   sPost = new String(sPost .getBytes("utf-8"),"8859_1");
  6. }

这个解决办法可以解决N800,Nokia6670等手机的中文输入乱码问题(一系列手机);
由于测试手机太少,不能断定该方法可以解决全部手机的中文乱码问题,只是权宜之计;
本测试和系统环境,以及程序编码环境等诸多因素相关;

大家还有什么好的建议和方法呢??

上一页  [1] [2] 

上一个:WAP与PHP - 进阶篇 之一
下一个:WAP 2.0--XHTML mobile profile

更多图片编程知识:
更多wap疑问解答:
新人求助QPainter
为什么程序都退出了还可以收到推送?如果大多设备都可以推送那运营商怎么办?
qt 4.7 sqlserver2000 存储过程调用
关于ANDROID4.0.1编译问题!
Android FrameBuffer读屏幕30秒后mmap失败
联通粗定位用java程序如何来请求和接受数据
为什么QT运行Android平台的程序时,mouseMoveEvent事件响应的间隔时间很长??????????
android与PC蓝牙通讯
指定大小的label 内容可变,如果内容超出label的宽度,将未能显示的部分显示在另一个label上
Android调试
android如何通过wifi连接无线打印机
运行程序,release目录下产生一个乱码文件夹
分享个某机构最新安卓资料,自己验证了
service启动不起来,掉不了service connection
求助:QT5.0 没有QPrinter吗
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,