最近写一个小爬虫;当多开了几个线程就会报错,一般报错的位置偏后:
SocketException: Software caused connection abort: recv failed
I/O exception (java.net.SocketException) caught when processing request: Software caused connection abort: recv failed
(java.net.SocketException) caught when processing request: Connection reset
在网上找了很久的资料,说是可能是客户端没有主动关闭连接。自己找了,但一直找不出问题所在;请各位大牛赐教。private InputStream doGetRequest(final HttpContext context) throws Exception {
HttpResponse response = null;
HttpClient httpclient = null;
try {
httpclient = new DefaultHttpClient();//BasicHttpClient.getHttpClient();
response = httpclient.execute(httprequest, context);
int status = response.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_OK) {
BasicEntry basicEntry = this.getBasicEntry(response);
return this.getContentStream(basicEntry);
} else {
throw new CrawlerException("http status error!" + status);
}
} catch (Exception e) {
throw e;
// throw new CrawlerException(e + " " + httprequest.getURI().toString());
}
finally{
 httprequest.releaseConnection();
 httpclient.getConnectionManager().closeExpiredConnections();
 httpclient.getConnectionManager().closeIdleConnections(0, TimeUnit.SECONDS);
 httpclient.getConnectionManager().shutdown();

}
}
private BasicEntry getBasicEntry(HttpResponse response) throws Exception {
InputStream in = null;
try {
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
HttpEntity entity =response.getEntity();  
if( entity != null ){   
in = entity.getContent(); 
}

if(in.available() <= 0){
int c = -1;
while ((c = in.read()) != -1) {
byteArray.write(c);
}
} byte[] bytes = byteArray.toByteArray(); String charset = EntityUtils.getContentCharSet(response.getEntity());
String contentType = null;
try {
contentType = response.getEntity().getContentType().getValue().toLowerCase();
} catch (Exception e) {
contentType = "text/html";
}
return new BasicEntry(bytes, contentType, charset);
} catch (Exception e) {
throw e;
}
finally{
 if (in != null){  
            try  
            {  
                in.close();  
            }  
            catch (IOException e)  
            {  
                e.printStackTrace ();  
            }
 }
}
}