我用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解析就没问题。
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解析就没问题。
new String(str.getBytes("ISO-8859-1"),"GB2312");
这样就可以搞定了,用GB2312编码!
...
System.out.print(new String(buffer.toString()
.getBytes("ISO-8859-1"), "utf-8"));...
再改下服务器的编码
servet.xml的URIEncoding该为UTF-8