1.先設置請求和超時時間:
/** * 讀超時設置30分鍾 */ private static int READTIMEOUT = 1800000; /** * 鏈接超時設置30秒 */ private static int CONNECTTIMEOUT = 30000;
2.GET請求,ServiceResult封裝過:
/** * 通用get方法 * @param addr * @param params * @return */ public static ServiceResult<Boolean> sendGet(String addr, Map<String, String> params) { String res = "error"; ServiceResult sr = new ServiceResult(false, res); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(addr); List<NameValuePair> values = Lists.newArrayList(); for (Map.Entry<String, String> entry : params.entrySet()) { values.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } String str = ""; UrlEncodedFormEntity uefEntity; try { str = EntityUtils.toString(new UrlEncodedFormEntity(values, Consts.UTF_8)); LOG.warn("executing request " + addr + "?" + str); //設置請求和傳輸超時時間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(READTIMEOUT).setConnectTimeout( CONNECTTIMEOUT).build(); httpGet.setConfig(requestConfig); CloseableHttpResponse response = httpclient.execute(httpGet); try { if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) { sr.setData(true); sr.setSucceed(true); } else { sr.setSucceed(false); sr.setData(false); } HttpEntity entity = response.getEntity(); if (entity != null) { res = EntityUtils.toString(entity, "UTF-8"); LOG.warn(res); } } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); LOG.warn("", e); } catch (UnsupportedEncodingException e) { LOG.warn("", e); } catch (IOException e) { LOG.warn("", e); } finally { // 關閉連接,釋放資源 try { httpclient.close(); } catch (IOException e) { LOG.warn("", e); } } return sr; }
3.POST請求:
/** * 通用post方法 * @param addr * @param params * @return */ public static ServiceResult<Boolean> sendPost(String addr, Map<String, String> params) { String res = "error"; ServiceResult sr = new ServiceResult(false, res); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httppost = new HttpPost(addr); List formparams = new ArrayList(); for (Map.Entry<String, String> entry : params.entrySet()) { formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } UrlEncodedFormEntity uefEntity; try { uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); httppost.setEntity(uefEntity); LOG.warn("executing request " + httppost.getURI()); //設置請求和傳輸超時時間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(READTIMEOUT).setConnectTimeout( CONNECTTIMEOUT).build(); httppost.setConfig(requestConfig); CloseableHttpResponse response = httpclient.execute(httppost); try { if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) { sr.setData(true); sr.setSucceed(true); } else { sr.setSucceed(false); sr.setData(false); } HttpEntity entity = response.getEntity(); if (entity != null) { res = EntityUtils.toString(entity, "UTF-8"); sr.setMsg(res); } } finally { response.close(); } } catch (ClientProtocolException e) { LOG.error("", e); } catch (UnsupportedEncodingException e) { LOG.error("", e); } catch (IOException e) { LOG.error("", e); } finally { // 關閉連接,釋放資源 try { httpclient.close(); } catch (IOException e) { LOG.error("", e); } } return sr; }