这是我在这里的第一篇文章。我是一个业余爱好者所以请耐心等待。
我正尝试使用以下代码从https://eztv.it/shows/1/24/抓取网页。
public static void WriteHTMLToFile(String URL){
try {
URI myURI=new URI(URL);
URL url = myURI.toURL();
HttpsURLConnection con= (HttpsURLConnection)url.openConnection();
File myFile=new File("c:\\project\\Test.txt");
myFile.createNewFile();
FileWriter wr=new FileWriter(myFile);
InputStream ins=con.getInputStream();
InputStreamReader isr= new InputStreamReader(ins);
BufferedReader reader = new BufferedReader(isr);
String line;
while ((line = reader.readLine()) != null) {
wr.write(line+"\n");
}
reader.close();
wr.close();
}
catch(Exception e){
log(e.toString());
}
}
当我运行时,我得到以下内容:
javax.net.ssl.SSLException:SSL对等关闭不正确
如果我在此网址上运行上述代码:https://eztv.it/shows/887/the-blacklist/它按预期工作。两个URL文件大小之间的差异似乎是一个促成因素。在测试同一服务器的不同URL时,上述代码似乎只适用于少于30Kb的文件。任何结果都会产生上述异常。
答案 0 :(得分:2)
我明白了。一旦文件大小超过一定大小,服务器就会使用gzip编码进行响应。
con.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
已添加到请求标头以及一些代码来处理gzip流。