解決Java處理HTTP請求超時的問題
在發送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+"連接無法關閉"); } }
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。
推薦閱讀:
- None Found