我用urlConnection这个类写了一个小程序public class TGCApply {
public static void main(String[] args) {
TGCApply jp = new TGCApply();
jp.doPost(); } public void doPost() {
try {
// URL url = new URL("http://192.168.210.66:9080/ssoua/login");
URL url = new URL("http://192.168.210.171:8088/KSSOUA/login");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection
.getOutputStream(), "UTF-8");
out
.write("UserName=li&Token=123456&AuthenticationType=0&ProcessCode=101");
out.flush();
out.close();
InputStream in = connection.getInputStream();
String encoding = connection.getContentEncoding();
System.out.println(encoding);
StringBuffer buffer = new StringBuffer();
BufferedReader bufferReader = new BufferedReader(
new InputStreamReader(in));
String line = "";
while ((line = bufferReader.readLine()) != null) {
buffer.append(line);
} System.out.println(buffer.toString());
System.out
.print(new String(buffer.toString().getBytes(), encoding));
System.out.print(new String(buffer.toString()
.getBytes("ISO-8859-1"), encoding));
} catch (Exception ex) {
}
}
}服务器端的写的传回的是一个随机中文字符串response.setCharacterEncoding("UTF-8");
response.setHeader("content-encoding","UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.setContentLength(new String("资源".getBytes(),"UTF-8").length());
response.getWriter().write(new String("资源".getBytes(),"UTF-8"));不过我怎么试都是服务器传回都是乱码,文件的编码都是UTF-8的,但是如果我用ISO-8859-1解析就没问题。

解决方案 »

  1.   


    new String(str.getBytes("ISO-8859-1"),"GB2312");
    这样就可以搞定了,用GB2312编码!
      

  2.   


    ...
    System.out.print(new String(buffer.toString()
                        .getBytes("ISO-8859-1"), "utf-8"));...
      

  3.   

    用一下filter吧 jsp版AJAX下拉条级联 这个项目里有关于filter的使用、配置
      

  4.   

    感觉FILTER就差不多了吧!
    再改下服务器的编码
      

  5.   

    不要使用gb2312,我发觉有些服务器版本对这个不支持,用gbk最好了。
      

  6.   

    不知道怎么回事,还是只有ISO-8859-1才能解决。
      

  7.   

    写个万能方法过滤下,可以直接用tommca带的那个就行,在把xml配置文件修改下就oK
      

  8.   

    页面编码统一为UF-8
    servet.xml的URIEncoding该为UTF-8