在发送POST或GET请求时,返回超时异常处理办法:
捕获 SocketTimeoutException | ConnectTimeoutException | ConnectionPoolTimeout 异常
三种异常说明:
SocketTimeoutException:是Java包下抛出的异常,这定义了Socket读数据的超时时间,即从server获取响应数据须要等待的时间;当读取或者接收Socket超时会抛出SocketTimeoutException。
ConnectTimeoutException:是Apache的HttpClient包抛出的超时异常,定义了通过网络与server建立连接的超时时间,Httpclient包中通过一个异步线程去创建与server的socket连接,这就是该socket连接的超时时;
当连接HTTPserver或者等待HttpConnectionManager管理的一个有效连接超时出错会抛出ConnectionTimeoutException。
ConnectionPoolTimeout:也是Apache的HttpClient包抛出的超时异常,定义了从 ConnectionManager 管理的连接池中取出连接的超时时间;出错会抛出 ConnectionPoolTimeoutException。
总结:
SocketTimeoutException异常是一个通用的异常,无论是用原生的HTTP请求,还是用Apache下的HttpClient包,在抛出的异常中都需要捕获 SocketTimeoutException 异常。
例:
public static String doGet(String url, Object params, String contentType) { try { return HttpUtils.doGetSend(url, params, contentType); } catch (SocketTimeoutException | ConnectTimeoutException e) { e.printStackTrace(); System.out.println("请求连接超时:" + e.getMessage()); } catch (IOException e) { e.printStackTrace(); System.out.println("请求异常,异常信息:" + e.getMessage()); } catch (Exception e) { e.printStackTrace(); } return null;}
补充:java 发送http请求(连接超时处理)
业务背景:
某项目跟第三方公司对接。
业务描述:
出于数据安全考虑,需要从服务器发送请求,来调用第三方公司提供的接口。但是该场景是销售类型,响应时间必须够快,那么就要设置响应的超时处理。
不然让客户看着圈圈在那里转半天,谁买?
项目架构:
jdk1.7
spring4.2.9
详细内容:
CloseableHttpClient httpclient = HttpClients.createDefault();try { HttpPost httpPost = new HttpPost(要访问的URL); //配置超时 RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(5000).setConnectionRequestTimeout(5000) .setSocketTimeout(5000).build(); httpPost.setConfig(requestConfig); //设置post请求参数 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("attr1", 参数值1)); nvps.add(new BasicNameValuePair("attr2", 参数值2)); nvps.add(new BasicNameValuePair("attr3", 参数值3)); …… httpPost.setEntity(new UrlEncodedFormEntity(nvps)); //执行post请求 CloseableHttpResponse response = httpclient.execute(httpPost); // 判断网络连接状态码是否正常(0--200都数正常) if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ //获取响应实体 responseMessage = EntityUtils.toString(response.getEntity(),"utf-8"); System.out.println(responseMessage); if(responseMessage!=null && !"".equals(responseMessage)){ String data = new String(AESUtil.decrypt(Base64.decode(responseMessage), key),"UTF-8"); String msg = translate_PRE(data,trans_type,transNo); result.put("msg", msg); } }else{ System.out.println("请求未成功响应: "+ response.getStatusLine()); } } catch (ConnectTimeoutException e) { System.out.println(api_type+"请求超时"); } catch (ClientProtocolException e) { System.out.println("请求失败"); } catch (Exception e) { e.printStackTrace(); } finally { //释放连接 try { if(httpclient!=null){ httpclient.close(); } } catch (IOException e) { System.out.println(api_type+"连接无法关闭"); } } .setConnectTimeout(5000).setConnectionRequestTimeout(5000) .setSocketTimeout(5000).build(); httpPost.setConfig(requestConfig); //设置post请求参数 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("attr1", 参数值1)); nvps.add(new BasicNameValuePair("attr2", 参数值2)); nvps.add(new BasicNameValuePair("attr3", 参数值3)); …… httpPost.setEntity(new UrlEncodedFormEntity(nvps)); //执行post请求 CloseableHttpResponse response = httpclient.execute(httpPost); // 判断网络连接状态码是否正常(0--200都数正常) if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ //获取响应实体 responseMessage = EntityUtils.toString(response.getEntity(),"utf-8"); System.out.println(responseMessage); if(responseMessage!=null && !"".equals(responseMessage)){ String data = new String(AESUtil.decrypt(Base64.decode(responseMessage), key),"UTF-8"); String msg = translate_PRE(data,trans_type,transNo); result.put("msg", msg); } }else{ System.out.println("请求未成功响应: "+ response.getStatusLine()); } } catch (ConnectTimeoutException e) { System.out.println(api_type+"请求超时"); } catch (ClientProtocolException e) { System.out.println("请求失败"); } catch (Exception e) { e.printStackTrace(); } finally { //释放连接 try { if(httpclient!=null){ httpclient.close(); } } catch (IOException e) { System.out.println(api_type+"连接无法关闭"); } }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。