SocketException: Software caused connection abort: recv failed

思年华 2012-10-11 05:47:48
最近写一个小爬虫;当多开了几个线程就会报错,一般报错的位置偏后:
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 ();
}
}
}
}
...全文
605 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
tangxingyou 2012-11-01
  • 打赏
  • 举报
回复
是不是jdk版本的问题,jdk1.7出现这个问题,但换成jdk1.6就好了。。。

67,543

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧