用java HttpURLConnection 读取网页中文乱码

发布网友 发布时间:2022-04-22 03:42

我来回答

5个回答

热心网友 时间:2024-01-10 10:50

出现这种情况,说明读取到的是iso8859-1格式,不能识别中文编码,所以需要转换一下编码格式才可以正常显示的,完整代码举例:
String data = "&version=1410&idType=101&idNumber=4107701214512&ext1=1";
System.out.println(data);
URL url = new URL("http://localhost/bankcardcert/bankCardValidate.htm");
StringBuffer bankXmlBuffer = new StringBuffer();
//创建URL连接,提交到银行卡鉴权,获取返回结果
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty("User-Agent", "directclient");
PrintWriter out = new PrintWriter(connection.getOutputStream());
out.println(data);
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection
.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
bankXmlBuffer.append(inputLine);
}
in.close();
String data = new String(bankXmlBuffer.toString().getBytes("iso-8859-1"), “utf-8”);
System.out.println(data);

热心网友 时间:2024-01-10 10:50

先看一下要获取网页的编码方式
new InputStreamReader(inputStream)这个类有构造方法可以指定文字编码,把编码指定一下就行了。
比如GBK 编码的页面就BufferedReader br = new BufferedReader( new InputStreamReader(inputStream,"GBK"));
UTF-8 的编码BufferedReader br = new BufferedReader( new InputStreamReader(inputStream,"UTF-8"));
也可以直接把项目的编码类型和页面的编码一致就能正常获取了。

热心网友 时间:2024-01-10 10:51

try {
URL url = new URL("");
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
InputStream inputStream = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(inputStream, "UTF-8");//添加这一句话设置相应编码格式

BufferedReader br=new BufferedReader(isr);
String temp=null;
while ((temp =br.readLine()) !=null) {
System.out.println(temp);
}
} catch (Exception e) {
}

热心网友 时间:2024-01-10 10:51

debug一下就知道;了

热心网友 时间:2024-01-10 10:52

网页的编码问题吧

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com