java urlconnection乱码_【求救贴】一个HttpUrlConnection发post请求,接收Json数据老是乱码的问题 - Java论坛 - 51CTO技术论坛_中国领先的IT技术...

public void getFeiInfo(String sqid) throws IOException{

//发送的请求参数,发送的格式也是Json的

String data = "{\"requestModule\":\"PatentSearch\",\"userId\":\"\",\"patentSearchConfig\":{\"Action\":\"loadLegal\",\"Query\":\"CN201310390507.8\"}}";

try {

//请求的地址

URL url = new URL("http://www.hnipo.net/client/interface.aspx");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

//发送Post强求,开启其读写的功能

conn.setDoOutput(true);

conn.setDoInput(true);

//这些参数是看了网上有人的建议,用上之后效果不大

conn.setRequestProperty("accpt", "        text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

conn.setRequestProperty("User-Agent", "        Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0");

conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");

conn.setRequestProperty("Connection", "        keep-alive");

conn.setRequestProperty("Cookie", "        SP.NET_SessionId=rtznindc2qwycf45ixyji2rr");

conn.setRequestMethod("POST");

//发送Post请求

conn.connect();

OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

//发送参数

writer.write(data);

writer.flush();

// 解析响应信息,用了很多种,用了字节流啊,处理流都试过了,这是最后试的那个

//主要是不能解决乱码问题,响应的东西还是能够收到,就是乱码

ByteArrayOutputStream baos=new ByteArrayOutputStream();

byte[] buf = new byte[1024];

int len = 0;

InputStream is = conn.getInputStream();

while((len=is.read(buf))!=-1){

System.out.println(len);

baos.write(buf, 0, len);

}

baos.flush();

//这个utf8给予了太多的希望

System.out.println(baos.toString("utf8"));

baos.close();

is.close();

} catch (MalformedURLException ex) {

ex.printStackTrace();

} catch (IOException ex) {

ex.printStackTrace();

}

}

已标记关键词 清除标记
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页