java post請求響應的時候字符亂碼

十指键魔 发布于 2017/02/26 18:22
阅读 175
收藏 0
package testSomethings;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

public class PostTest2 {
	public static void httpUrlConnection() {
		try {
			
			Date date1 = new Date();
			
			// String pathUrl =
			// "http://tx3.cbg.163.com/cgi-bin/overall_search.py";
			String pathUrl = "http://tx3.cbg.163.com/cgi-bin/equipquery.py?act=show_overall_search";
			// 建立连接
			URL url = new URL(pathUrl);
			HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

			// //设置连接属性
			httpConn.setDoOutput(true);// 使用 URL 连接进行输出
			httpConn.setDoInput(true);// 使用 URL 连接进行输入
			httpConn.setUseCaches(false);// 忽略缓存
			httpConn.setRequestMethod("POST");// 设置URL请求方法
			String requestString = "客服端要以以流方式发送到服务端的数据...";
			
			String charset = "GB2312";
			// 设置请求属性
			// 获得数据字节数据,请求数据流的编码,必须和下面服务器端处理请求流的编码一致
			byte[] requestStringBytes = requestString.getBytes();

			httpConn.setRequestProperty("Charset", charset);

			httpConn.setRequestProperty("Accept", "*/*");
			httpConn.setRequestProperty("Accept-Encoding", "gzip, deflate");
			httpConn.setRequestProperty("Accept-Language", "zh-Hans-CN, zh-Hans; q=0.7, ja; q=0.3");
			httpConn.setRequestProperty("Cache-Control", "no-cache");
			httpConn.setRequestProperty("Connection", "Keep-Alive");
			httpConn.setRequestProperty("Content-Length", "118");
			httpConn.setRequestProperty("Content-Type", "text/plain; charset=UTF-8");

			httpConn.setRequestProperty("Host", "tx3.cbg.163.com");
			//
			httpConn.setRequestProperty("school", "1");
			httpConn.setRequestProperty("level_min", "1");
			httpConn.setRequestProperty("level_max", "80");
			httpConn.setRequestProperty("price_min", "80");
			httpConn.setRequestProperty("price_max", "500");
			httpConn.setRequestProperty("order_by", "");
			httpConn.setRequestProperty("page", "1");
			httpConn.setRequestProperty("other_arg", "");

			// 建立输出流,并写入数据
			OutputStream outputStream = httpConn.getOutputStream();
			outputStream.write(requestStringBytes);
			outputStream.close();
			// 获得响应状态
			int responseCode = httpConn.getResponseCode();

			if (HttpURLConnection.HTTP_OK == responseCode) {// 连接成功
				// 当正确响应时处理数据
				StringBuffer sb = new StringBuffer();
				String readLine;
				BufferedReader responseReader;
				// 处理响应流,必须与服务器响应流输出的编码一致
				responseReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), charset));
				while ((readLine = responseReader.readLine()) != null) {
					sb.append(readLine);
				}
				responseReader.close();
				System.out.println(sb.toString());
				Date date2 = new Date();
				System.out.println(date2.getTime()-date1.getTime());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	public static void main(String[] args) {
		httpUrlConnection();
	}
}


如何讓它響應的時候不出現亂碼啊
GBK GB2312  UTF8  都試過了啊

 

 

加载中
0
kakai
kakai
byte[] requestStringBytes = requestString.getBytes();

要改成requestString.getBytes(charset)这个,不然默认是系统的默认编码将字符串转为字节的

kakai
kakai
回复 @十指键魔 : 建议你使用httpclient组件,它是HttpURLConnection的封装,比直接使用HttpURLConnection简单多了
十指键魔
十指键魔
回复 @BoXuan : 有空 能不能帮忙改下代码 帮忙 解决下乱码问题就好了啊 http://tx3.cbg.163.com/cgi-bin/equipquery.py?act=show_overall_search 我这个就是做着玩的 网站上面的检索条件 太简陋了啊
十指键魔
十指键魔
回复 @BoXuan : 这个东西多少长度我是 浏览器上看到的啊 。。。 真实访问的时候
kakai
kakai
回复 @十指键魔 : Content-Length不能设置为固定的118,而是发送数据的字节长度,你这里应该是发送的字符串转字节后的字节长度
kakai
kakai
回复 @十指键魔 : 那就用httpclient吧
下一页
0
草的高度
草的高度

把这里去掉就没问题了httpConn.setRequestProperty("Accept-Encoding", "gzip, deflate")。

这个请求头拿到的数据是压缩的,所以你怎么设置编码都是乱码。

返回顶部
顶部