使用HttpUrlConnetion读取html页面,因为不知道html的编码,第一次按默认编码读取到一个字符串中,然后再根据该字符串获得html的字符编码,再使用该字符编码再次读取,但是这里的输入流(connection.getInputStream());)在第一个while处已经读到末尾了,因此在第二个while循环中,已经到末尾一下子就跳出了,page输出为"",解决方法是在注释出再次connect Http服务器,我希望只connect 一次就行。HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
String page = "";
BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {//把html全部读到page中
page += new String(buffer);
}
String charset = getCharset(page);//解析html,获得字符编码
//connection = (HttpURLConnection) url.openConnection();
//connection.connect();
BufferedInputStream in2 = new BufferedInputStream(connection.getInputStream());//再次从 page = "";
byte[] buffer2 = new byte[1024];
while (in2.read(buffer2) != -1) {//一下子跳出while
page += new String(buffer2, charset);
}
connection.connect();
String page = "";
BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {//把html全部读到page中
page += new String(buffer);
}
String charset = getCharset(page);//解析html,获得字符编码
//connection = (HttpURLConnection) url.openConnection();
//connection.connect();
BufferedInputStream in2 = new BufferedInputStream(connection.getInputStream());//再次从 page = "";
byte[] buffer2 = new byte[1024];
while (in2.read(buffer2) != -1) {//一下子跳出while
page += new String(buffer2, charset);
}
reset();
?
你这样的写法in2永远得不到流 流都被第一个while读了 服务器在输出流后都会flush()操作的
学习了, 同事也曾遇到过类似的问题!
connection.connect();
String page = "";
BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
in.(connection.getContentLength() + 1);//标志
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {//把html全部读到page中
page += new String(buffer);
}
String charset = getCharset(page);//解析html,获得字符编码
in.reset();//重置输入流
if (!in.Supported()) {
(HttpURLConnection) url.openConnection();
connection.connect();
in = new BufferedInputStream(connection.getInputStream());
}
page = "";
while (in.read(buffer) != -1) {
page += new String(buffer, charset);
}