用java写了一个Http client,但向服务器post的时候传中文参数老是乱码,请大侠明示一下

来源:百度知道 编辑:UC知道 时间:2024/06/24 07:20:56
...
con.setRequestProperty( "Content-Type","application/x-www-form-urlencoded" );
//这样加属性也是不行
...
String para = "Title=中国"+"&Data=人民";
//URLEncoder.encode(para)也不行
...
con.setDoOutput( true );
OutputStreamWriter out = new OutputStreamWriter( con.getOutputStream() );
out.write( para );
out.flush();

请大侠指点一下!
谢谢!
补充一下啊 ,服务器端我没法改的,只能在客户端想办法!!!!!!!!!

以上的2个方法最好都要用上 过滤器只能解决POST请求 ,要处理GET请求就要用
bytes = string.getBytes("iso-8859-1") 得到原始的字节串,再用 string = new String(bytes, "GB2312") 重新得到正确的字符串 。
这个方法,所以最好2个都要写,这样不管是POST还是GET请求就都能解决了。

写个过滤器就行了。将编码格式写在web.xml的配置文件里。
下面的这个过滤器是tomcat提供的。
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under