用java HttpURLConnection 读取网页中文乱码
读取网页内容保存为本地字符串,为什么中文都是乱码?代码如下:publicstaticStringgetStringFromURL(StringurlString)thro...
读取网页内容保存为本地字符串,为什么中文都是乱码?
代码如下:
public static String getStringFromURL(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection connection =(HttpURLConnection)url.openConnection();
InputStream inputStream = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer sb = new StringBuffer();
String line = null;
while((line=br.readLine())!=null)
{
sb.append(line+System.getProperty("line.separator"));
}
return sb.toString();
} 展开
代码如下:
public static String getStringFromURL(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection connection =(HttpURLConnection)url.openConnection();
InputStream inputStream = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer sb = new StringBuffer();
String line = null;
while((line=br.readLine())!=null)
{
sb.append(line+System.getProperty("line.separator"));
}
return sb.toString();
} 展开
5个回答
展开全部
出现这种情况,说明读取到的是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);
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);
展开全部
先看一下要获取网页的编码方式
new InputStreamReader(inputStream)这个类有构造方法可以指定文字编码,把编码指定一下就行了。
比如GBK 编码的页面就BufferedReader br = new BufferedReader( new InputStreamReader(inputStream,"GBK"));
UTF-8 的编码BufferedReader br = new BufferedReader( new InputStreamReader(inputStream,"UTF-8"));
也可以直接把项目的编码类型和页面的编码一致就能正常获取了。
new InputStreamReader(inputStream)这个类有构造方法可以指定文字编码,把编码指定一下就行了。
比如GBK 编码的页面就BufferedReader br = new BufferedReader( new InputStreamReader(inputStream,"GBK"));
UTF-8 的编码BufferedReader br = new BufferedReader( new InputStreamReader(inputStream,"UTF-8"));
也可以直接把项目的编码类型和页面的编码一致就能正常获取了。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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) {
}
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) {
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
debug一下就知道;了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
网页的编码问题吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询