package com.icss.daas.core.util; import java.util.Iterator; import java.util.Map; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.logging.log4j.Logger; import com.alibaba.fastjson.JSONObject; import org.apache.logging.log4j.LogManager; public class HttpClientUtil { static Logger logger = LogManager.getLogger(HttpClientUtil.class); //static Logger logger = LogManager.getLogger(HttpClientUtil.class); private static String DEFAULT_CHARSET = "UTF-8"; private static int DEFAULT_CONNECTION_TIMEOUT = 10 * 1000; private static int DEFAULT_SO_TIMEOUT = 10 * 1000; public static String addUrl(String head, String tail) { if (head.endsWith("/")) { if (tail.startsWith("/")) { return head.substring(0, head.length() - 1) + tail; } else { return head + tail; } } else { if (tail.startsWith("/")) { return head + tail; } else { return head + "/" + tail; } } } /** * post請求數據 * @param url * @param params * @param charset * @return * @throws Exception */ public synchronized static String postData(String url, JSONObject json, String charset) throws Exception { //構造HttpClient的實例 HttpClient httpClient = new HttpClient(); //建立post方法的實例 PostMethod method = new PostMethod(url); charset=StringUtils.isBlank(charset)? DEFAULT_CHARSET:charset; ((PostMethod) method).addParameter("json", json.toString()); HttpMethodParams httpMethodParam = method.getParams(); httpMethodParam.setContentCharset("UTF-8"); httpMethodParam.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset); String result = ""; try { httpClient.executeMethod(method);// 執行postMethod result=new String(method.getResponseBody(),charset); } catch (Exception e) { throw e; }finally{ //釋放鏈接 method.releaseConnection(); } return result; } public synchronized static String postData(String url, Map<String,String> header,JSONObject json, String charset) throws Exception { //構造HttpClient的實例 HttpClient httpClient = new HttpClient(); //建立post方法的實例 PostMethod method = new PostMethod(url); charset=StringUtils.isBlank(charset)? "utf-8":charset; ((PostMethod) method).addParameter("cdata", json.toString()); HttpMethodParams httpMethodParam = method.getParams(); httpMethodParam.setContentCharset("UTF-8"); httpMethodParam.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset); // 添加請求頭 Iterator<String> iter = header.keySet().iterator(); String key = null; while(iter.hasNext()) { key = iter.next(); method.addRequestHeader(key,header.get(key)); } String result = ""; try { httpClient.executeMethod(method);// 執行postMethod result=new String(method.getResponseBody(),charset); } catch (Exception e) { logger.error("HTTP以POST請求數據異常", e); throw e; }finally{ //釋放鏈接 method.releaseConnection(); } return result; } /** * get請求數據 * @param url * @param params * @param charset * @return * @throws Exception */ public synchronized static String getData(String url, Map<String, String> params, String charset) throws Exception { final HttpClient httpClient = new HttpClient(); httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT); httpClient.getHttpConnectionManager().getParams().setSoTimeout(DEFAULT_SO_TIMEOUT); final GetMethod method = new GetMethod(url); String result = ""; try { httpClient.executeMethod(method); charset=StringUtils.isBlank(charset)? DEFAULT_CHARSET:charset; result=new String(method.getResponseBody(),charset); } catch (Exception e) { logger.info("HTTP以GET請求數據異常", e); throw e; }finally{ method.releaseConnection(); } return result; } }