我在进行GET请求时,由与参数是中文的,传到IIS上时变成乱码,我的IIS页面是UTF-8的,所以参数也该是UTF-8,我应该怎么样把我的字符串 转 成 UTF8格式的参数呢?
new String((new String(str)).getBytes("gb2321"),"utf-8"); 这样似乎不对,我也换过几个编码,也不对,不太理解,到底该怎么做?请指点一二。
------解决方案--------------------------------------------------------
GET时如果URL太长, 移动网关会给你截断的
不过貌似GET比POST速度要快点
字符编码的话, 你还是根据j2me里面的utf-8, 自己写个函数来转换吧, 把char转成byte
If a character c is in the range \u0001 through \u007f, it is represented by one byte:
(byte)c If a character c is \u0000 or is in the range \u0080 through \u07ff, then it is represented by two bytes, to be written in the order shown:
(byte)(0xc0 | (0x1f & (c >> 6)))
(byte)(0x80 | (0x3f & c))
If a character c is in the range \u0800 through uffff, then it is represented by three bytes, to be written in the order shown:
(byte)(0xe0 | (0x0f & (c >> 12)))
(byte)(0x80 | (0x3f & (c >> 6)))
(byte)(0x80 | (0x3f & c))
------解决方案--------------------------------------------------------
1.对参数做UrlEncode
2.str.getBytes("utf-8")得到一个byte数组,把这个数组传输
------解决方案--------------------------------------------------------
贴个urlEncode方法
- Java code
public static String urlEncode(String url) { final String hexChars="0123456789ABCDEF";//16进制符号 StringBuffer sb=new StringBuffer(); for(int i=0;i<url.length();i++) { char c=url.charAt(i); if(c<128) sb.append(c);//小于128的ascii符号保留 else//非ascii符号转化成字节数组,并以%开头 { String temp=String.valueOf(c); byte[] b=temp.getBytes(); for(int j=0;j<b.length;j++) { sb.append('%'); sb.append(hexChars.charAt((b[j]&0xf0)>>4)); sb.append(hexChars.charAt(b[j]&0x0f)); } } } return sb.toString(); }
------解决方案--------------------------------------------------------
……楼上的代码跟方法让我很郁闷。
j2me中的utf-8跟别处的utf-8虽然名称一样,但实际是不同的。
j2me中会多两个字节,表示整个字符的长度,放在编码的开头。
所以,如果你使用readUTF()时,就要小心了,它只认有头的。不过这个知识点对你的问题没影响。你需要把中文参数转换成为URLEncode
不过上面的代码似乎不够,你可以看看我的文章。是从java中的源代码修改而来的。
http://blog.csdn.net/hunhun1981/archive/2007/09/26/1801224.aspx
------解决方案--------------------------------------------------------
if("GET".equalsIgnoreCase(request.getMethod())){
Map map = request.getParameterMap();
for(Iterator it = map.keySet().iterate();it.hasNext();){
String[] value = (String[])it.next();
value[0] = new String(value[0].getBytes("ISO8859-1"),"GB2312");
}
}
这是我就在上面写的代码...有可能有些单词写错了..
请核对哦.
这个方法我用了好多次了,每个项目都用过哦...
提出来和大家分享一下啊