JAVA通用GET和POST方法
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; }
随笔看心情