package com.tenwa.httpclient; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.httpclient.ConnectTimeoutException; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import com.amarsoft.are.ARE; import com.tenwa.httpclient.resources.GPSConfigure; public class HttpClientUtil { public static String getData(String url, String methodType, Map params,boolean flag) throws Exception { long start = System.currentTimeMillis(); String result = ""; switch(methodType.toUpperCase()) { case "GET": result = loadDataGet(url, flag); break; case "POST": result = loadDataPost(url, params, flag); break; default: throw new RuntimeException("wrong method type"); } long end = System.currentTimeMillis(); ARE.getLog().info("response time : " + (end - start) + "ms"); return result; } /** * 通过POST的方式获取获得数据 * @param url * @param params * @return * @throws Exception */ private static String loadDataPost(String url,Map params, boolean flag) throws Exception { HttpClient httpClient = new HttpClient(); String result = ""; PostMethod postMethod = null; try { postMethod = new PostMethod(url); for(Entry param : params.entrySet()) { postMethod.addParameter(param.getKey(), param.getValue()); } if(flag) { postMethod.setRequestHeader("appkey", GPSConfigure.get("APPKEY")); } postMethod.getParams().setParameter( HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); int statusCode = httpClient.executeMethod(postMethod); if (statusCode == HttpStatus.SC_OK) { InputStream txtis = postMethod.getResponseBodyAsStream(); BufferedReader br = new BufferedReader(new InputStreamReader(txtis, "UTF-8")); StringBuffer sbf = new StringBuffer(); String line = null; while ((line = br.readLine()) != null) { sbf.append(line); } result = sbf.toString(); ARE.getLog().info(result); } else { throw new RuntimeException(statusCode + ""); } } catch(Exception e) { throw new RuntimeException(e); } finally { if(postMethod != null) postMethod.releaseConnection(); } return result; } /** * 通过GET的方式获取数据 * @param url * @param params * @return * @throws Exception */ private static String loadDataGet(String url, boolean flag) throws Exception { HttpClient httpClient = new HttpClient(); String result = ""; GetMethod postMethod = null; try { postMethod = new GetMethod(url); if(flag) { postMethod.setRequestHeader("appkey", GPSConfigure.get("APPKEY")); } postMethod.getParams().setParameter( HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); int statusCode = httpClient.executeMethod(postMethod); if (statusCode == HttpStatus.SC_OK) { InputStream txtis = postMethod.getResponseBodyAsStream(); BufferedReader br = new BufferedReader(new InputStreamReader(txtis, "UTF-8")); StringBuffer sbf = new StringBuffer(); String line = null; while ((line = br.readLine()) != null) { sbf.append(line); } result = sbf.toString(); ARE.getLog().info(result); } else { throw new RuntimeException(statusCode + ""); } ARE.getLog().info(result); } catch(Exception e) { throw new RuntimeException(e); } finally { if(postMethod != null) postMethod.releaseConnection(); } return result; } /** * 通过使用CloseableHttpClient的 * @param url * @param params * @return */ private static String getDataPost(String url,Map params) { RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(5000).setConnectionRequestTimeout(1000) .build(); CloseableHttpClient httpclient = null; CloseableHttpResponse result = null; String resultString = ""; try { httpclient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build(); HttpPost postMethod = new HttpPost(url); /*postMethod .setHeader( "User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36");*/ postMethod.setHeader("Referer", url); List nvps = new ArrayList(); for(Entry param : params.entrySet()) { nvps.add(new BasicNameValuePair(param.getKey(), param.getValue())); } // 添加参数 postMethod.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); result = httpclient.execute(postMethod); HttpEntity repEntity = result.getEntity(); int statusCode = result.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { return "410"; } resultString = EntityUtils.toString(repEntity, "UTF-8"); ARE.getLog().info(resultString); } catch (ConnectTimeoutException e) { throw new RuntimeException(e); } catch (Exception e) { throw new RuntimeException(e); } finally { if (result != null) { try { result.close(); } catch (IOException e) { e.printStackTrace(); } } if (httpclient != null) { try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } return resultString; } }