- package com.weare09.util;
- import java.io.BufferedReader;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- import java.util.Map;
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.HttpStatus;
- import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
- import org.apache.commons.httpclient.methods.PostMethod;
- import org.apache.commons.httpclient.methods.RequestEntity;
- import org.apache.commons.httpclient.methods.StringRequestEntity;
- import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
- import org.dom4j.Document;
- import org.dom4j.DocumentHelper;
- import org.dom4j.Element;
- import com.weare09.init.InitProperties;
- public class HttpClientUtil {
- private HttpClient httpClient = null;
- private Object lock = new Object();
- /**
- * 获取httpClient
- * @return
- */
- private HttpClient getHttpClient() {
- if (httpClient != null) {
- return httpClient;
- }
- synchronized (lock) {
- if (httpClient == null) {
- try {
- initHttpClient();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- return httpClient;
- }
- /**
- * httpClient初始化
- */
- private void initHttpClient() {
- MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
- int readTimeOut = InitProperties.HTTP_SO_TIME_OUT;
- int connTimeOut = InitProperties.HTTP_CONNECT_TIME_OUT;
- int maxConnPerHost = InitProperties.HTTP_MAX_CONN_PER_HOST;
- int maxTotalConns = InitProperties.HTTP_MAX_TOTAL_CONNS;
- long connManagerTimeOut = InitProperties.HTTP_CONN_MGR_TIME_OUT;
- //参数设置
- HttpConnectionManagerParams params = new HttpConnectionManagerParams();
- params.setConnectionTimeout(connTimeOut);
- params.setSoTimeout(readTimeOut);
- params.setDefaultMaxConnectionsPerHost(maxConnPerHost);
- params.setMaxTotalConnections(maxTotalConns);
- manager.setParams(params);
- httpClient = new HttpClient(manager);
- httpClient.getParams().setConnectionManagerTimeout(connManagerTimeOut);
- }
- /**
- * 发送消息,获取响应消息体
- * @param url
- * @param reqXml
- * @param headMap
- * @return
- */
- public Element postReq(String url,String reqXml,HashMap<String, String> headMap){
- PostMethod method = new PostMethod(url);
- try {
- //设置消息头
- if(headMap!=null&&headMap.size()>0){
- for(Map.Entry<String, String> entry:headMap.entrySet()){
- method.setRequestHeader(entry.getKey(), entry.getValue());
- }
- }
- //设置消息体
- RequestEntity requestEntity = new StringRequestEntity(reqXml, "text/xml", "utf-8");
- method.setRequestEntity(requestEntity);
- //发消息
- int httpResult = getHttpClient().executeMethod(method);
- if(httpResult==HttpStatus.SC_OK){
- Element element = getElementAsStream(method.getResponseBodyAsStream());
- return element;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- method.releaseConnection();
- }
- return null;
- }
- /**
- * 获取IO流中的消息体
- * @param stream
- * @return
- * @throws Exception
- */
- public Element getElementAsStream(InputStream stream) throws Exception{
- BufferedReader in = new BufferedReader(new InputStreamReader(stream));
- StringBuffer stringBuffer = new StringBuffer();
- String buffer = "";
- String reponse = "";
- while(null!=(buffer=(in.readLine()))){
- stringBuffer.append(buffer).append("\\r\\n");
- }
- reponse = stringBuffer.toString();
- Document doc = DocumentHelper.parseText(reponse);
- Element element = doc.getRootElement();
- return element;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2601201614498.html
来源: http://www.codesnippet.cn/detail/2601201614498.html