httpclient调用api接口出现java.net.ConnectException: Connection timed out解决方法
httpclient调用api接口出现java.net.ConnectException: Connection timed out
如题,根据日志调用接口时有时会报这种异常,各位大神帮忙解答下。调用api接口的代码如下:
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
HttpGet method = new HttpGet(url);
try {
HttpResponse result = client.execute(method);
if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = result.getEntity();
this.readXml(EntityUtils.toString(entity, "GBK"),stationId,latandlng[5], latandlng[6]);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Date date=new Date();
DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=format.format(date);
logger.error("异常时间:"+time);
logger.error("异常站号:"+stationId);
logger.error("异常url:"+url);
logger.error("exception:",e);
continue;
}finally{
client.getConnectionManager().shutdown();
}
------解决思路----------------------
以前我做过一个并发登录的程序,当时只开了10个线程也会出现这样的问题,不过不是很频繁;
如果想解决这个问题,我觉得需要一个更高效的网络框架,可能httpClient不太适合高并发吧。
或者你可以干脆忽略这些错误,让它再请求一次就好了(类似于打不开网页刷个新)。
如题,根据日志调用接口时有时会报这种异常,各位大神帮忙解答下。调用api接口的代码如下:
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
HttpGet method = new HttpGet(url);
try {
HttpResponse result = client.execute(method);
if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = result.getEntity();
this.readXml(EntityUtils.toString(entity, "GBK"),stationId,latandlng[5], latandlng[6]);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Date date=new Date();
DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time=format.format(date);
logger.error("异常时间:"+time);
logger.error("异常站号:"+stationId);
logger.error("异常url:"+url);
logger.error("exception:",e);
continue;
}finally{
client.getConnectionManager().shutdown();
}
------解决思路----------------------
以前我做过一个并发登录的程序,当时只开了10个线程也会出现这样的问题,不过不是很频繁;
如果想解决这个问题,我觉得需要一个更高效的网络框架,可能httpClient不太适合高并发吧。
或者你可以干脆忽略这些错误,让它再请求一次就好了(类似于打不开网页刷个新)。