我正在编写一个Java程序,它使用Apache-HttpComponents加载页面并将其HTML打印到控制台;但是,程序只会在抛出此错误之前打印部分HTML:Exception in thread "main" java.net.SocketException: socket closed.
每次运行程序时,异常之前显示的HTML部分都完全相同,并且在此简化示例中出现错误,雅虎和Craigslist:
String USERAGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22";
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://www.craigslist.org");
get.setHeader(HTTP.USER_AGENT,USERAGENT);
HttpResponse page = client.execute(get);
get.releaseConnection();
InputStream stream = page.getEntity().getContent();
try{
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
String line = "";
while ((line = br.readLine()) != null){
System.out.println(line);
}
}
finally{
EntityUtils.consume(page.getEntity());
}
答案 0 :(得分:5)
我发现在我读完HTML之前不应该调用get.releaseConnection();
。在EntityUtils.consume(page.getEntity());
修复上述代码后立即调用它。