HttpClient 代码样例()

package com.kingdee.eas.custom.cfinv.utils;

import com.kingdee.bos.ui.face.CoreUIObject;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

public class HttpUtils {

      private static Logger logger = CoreUIObject.getLogger(HttpUtils.class);

      public static HttpClient getHttpClient()
      {
        CloseableHttpClient httpClient = HttpClients.custom()
          .setConnectionManager(ConnectionManager.manager).build();
        return httpClient;
      }

      public static JSONObject doPost(String url, String params, String charset) throws Exception
      {
        logger.error("HttpUtils:start-----doPost");
        HttpClient hc = getHttpClient();
        HttpPost httpPost = new HttpPost(url);

        BufferedReader bufferedReader = null;
        try {
          logger.error("获取到response之前");
          StringEntity se = new StringEntity(params, charset);
          se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
          httpPost.setEntity(se);

          HttpResponse response = hc.execute(httpPost);
          HttpEntity e = response.getEntity();
          StringBuffer buffer = new StringBuffer();
          logger.error("获取到response之后");
          if (e != null) {
            Long len = Long.valueOf(e.getContentLength());
            if ((len.longValue() != -1L) && (len.longValue() < 2048L)) {
                logger.error("2048L");
                logger.error("2048L"+buffer.toString());
              buffer.append(EntityUtils.toString(e, charset));
            } else {
                logger.error("追加"+buffer.toString());
              bufferedReader = new BufferedReader(
                new InputStreamReader(e.getContent(), charset));
              String str = null;
              while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
              }
            }
          }
            logger.error("报文:"+buffer.toString());
          if (response.getStatusLine().getStatusCode() >= 300) {
              logger.error("code大于300:"+buffer.toString());
            throw new HttpResponseException(
              response.getStatusLine().getStatusCode(), 
              response.getStatusLine().getReasonPhrase());
          }

          if (e == null) {
              logger.error("返回内容为空");
            throw new ClientProtocolException("返回内容为空");
          }

          logger.error("HttpUtils.doPost=\r\n"+response.getStatusLine().getStatusCode() + "\n" + 
            buffer.toString());
        
          JSONObject resultObject = new JSONObject(buffer.toString());
          logger.error("HttpUtils.resultObject=\r\n"+resultObject);
          return resultObject;
        }
        catch (Exception e) {
            logger.error("doPost错误:"+e.getMessage(), e);
            throw new Exception(e);
        }
        finally {
          if (bufferedReader != null) {
            try {
              bufferedReader.close();
            } catch (IOException e) {
              e.printStackTrace();
              logger.error("dopost错误:"+e,e);
              throw new RuntimeException(e);
            }
          }
          logger.error("dopost关闭");
//          httpPost.releaseConnection();
        }
      }

      public static byte[] downloadFile(String url) {
          logger.error("HttpUtils:start-----downloadFile");
        HttpClient hc = getHttpClient();
        HttpGet httpGet = new HttpGet(url);
        InputStream in = null;
        byte[] b = (byte[])null;
        try {
          HttpResponse response = hc.execute(httpGet);
          HttpEntity e = response.getEntity();
          if (response.getStatusLine().getStatusCode() >= 300) {
            throw new HttpResponseException(
              response.getStatusLine().getStatusCode(), 
              response.getStatusLine().getReasonPhrase());
          }
          if (e != null) {
            in = e.getContent();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int l = 0;
            byte[] tmp = new byte[1024];
            while ((l = in.read(tmp)) != -1) {
              out.write(tmp, 0, l);
            }
            b = out.toByteArray();
            out.close();
          } else {
            throw new ClientProtocolException("返回内容为空");
          }
          return b;
        }
      
        catch (Exception e) {
        }
        finally {
          if (in != null) {
            try {
              in.close();
            } catch (IOException e) {
              e.printStackTrace();
              logger.error("downloadFile错误:"+e,e);
              throw new RuntimeException(e);
            }
          }
//          httpGet.releaseConnection();
        }
        return null;
      }

      public static String encode(String[] args) {
        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        if ((args == null) || (args.length == 0)) return "";
        StringBuffer sb = new StringBuffer();
        for (String str : args) {
          sb.append(str);
        }
        byte[] bytes = sb.toString().getBytes();
        try {
          MessageDigest mdInst = MessageDigest.getInstance("MD5");
          mdInst.update(bytes);
          byte[] md = mdInst.digest();
          char[] ciphertext = new char[2 * md.length];
          int k = 0;
          for (int j = 0; j < md.length; ++j) {
            byte b0 = md[j];
            ciphertext[(k++)] = hexDigits[(b0 >>> 4 & 0xF)];
            ciphertext[(k++)] = hexDigits[(b0 & 0xF)];
          }
          return new String(ciphertext);
        } catch (NoSuchAlgorithmException e) {
          e.printStackTrace();
          logger.error("encode错误:"+e,e);
          throw new RuntimeException(e);
        }
      }

      public static JSONObject doGet(String url, String charset)
      {
          logger.error("HttpUtils:start-----doGet");
        HttpClient hc = getHttpClient();
        HttpGet httpGet = new HttpGet(url);
        BufferedReader bufferedReader = null;
        try {
          HttpResponse response = hc.execute(httpGet);
          HttpEntity e = response.getEntity();
          StringBuffer buffer = new StringBuffer();
          if (e != null) {
            Long len = Long.valueOf(e.getContentLength());
            if ((len.longValue() != -1L) && (len.longValue() < 2048L)) {
              buffer.append(EntityUtils.toString(e, charset));
            } else {
              bufferedReader = new BufferedReader(
                new InputStreamReader(e.getContent(), 
                charset));
              String str = null;
              while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
              }
            }
          }
          if (response.getStatusLine().getStatusCode() >= 300) {
            throw new HttpResponseException(response.getStatusLine().getStatusCode(), 
              response.getStatusLine().getReasonPhrase());
          }
          if (e == null) {
            throw new ClientProtocolException("返回内容为空");
          }
          logger.error("doGet报文状态码"+response.getStatusLine().getStatusCode() + "\n" + buffer.toString());
          logger.info(response.getStatusLine().getStatusCode() + "\n" + buffer.toString());

          return new JSONObject(buffer.toString());
        }
        catch (UnsupportedEncodingException e1)
        {
            logger.error("doGet错误:"+e1,e1);
        }
        catch (ClientProtocolException e)
        {
            logger.error("doGet错误:"+e,e);
        }
        catch (IOException e)
        {
            logger.error("doGet错误:"+e,e);
        }
        catch (JSONException e) {
            logger.error("doGet错误:"+e,e);
        }
        finally {
          if (bufferedReader != null) {
            try {
              bufferedReader.close();
            } catch (IOException e) {
              e.printStackTrace();
              logger.error("doGet错误:"+e,e);
              throw new RuntimeException(e);
            }
          }
//          httpGet.releaseConnection();
        }
        return null;
      }

      public static JSONObject doPost(String url, List<NameValuePair> params, String charset)
      {
        HttpClient hc = getHttpClient();
        HttpPost httpPost = new HttpPost(url);

        BufferedReader bufferedReader = null;
        try {
          StringEntity se = new UrlEncodedFormEntity(params, "UTF-8");
          se.setContentEncoding(new BasicHeader("Content-Type", "application/x-www-form-urlencoded"));
          httpPost.setEntity(se);

          HttpResponse response = hc.execute(httpPost);
          HttpEntity e = response.getEntity();
          StringBuffer buffer = new StringBuffer();
          if (e != null) {
            Long len = Long.valueOf(e.getContentLength());
            if ((len.longValue() != -1L) && (len.longValue() < 2048L)) {
              buffer.append(EntityUtils.toString(e, charset));
            } else {
              bufferedReader = new BufferedReader(
                new InputStreamReader(e.getContent(), charset));
              String str = null;
              while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
              }
            }
          }
          if (response.getStatusLine().getStatusCode() >= 300) {
            throw new HttpResponseException(
              response.getStatusLine().getStatusCode(), 
              response.getStatusLine().getReasonPhrase());
          }

          if (e == null) {
            throw new ClientProtocolException("返回内容为空");
          }

          logger.info(response.getStatusLine().getStatusCode() + "\n" + 
            buffer.toString());
          return new JSONObject(buffer.toString());
        } catch (Exception e) {
        }
        finally {
          if (bufferedReader != null) {
            try {
              bufferedReader.close();
            } catch (IOException e) {
              e.printStackTrace();
              throw new RuntimeException(e);
            }
          }
//          httpPost.releaseConnection();
        }
        return null;
      }

      public static String doGet4String(String url, String charset)
      {
        HttpClient hc = getHttpClient();
        HttpGet httpGet = new HttpGet(url);
        BufferedReader bufferedReader = null;
        try {
          HttpResponse response = hc.execute(httpGet);
          HttpEntity e = response.getEntity();
          StringBuffer buffer = new StringBuffer();
          if (e != null) {
            Long len = Long.valueOf(e.getContentLength());
            if ((len.longValue() != -1L) && (len.longValue() < 2048L)) {
              buffer.append(EntityUtils.toString(e, charset));
            } else {
              bufferedReader = new BufferedReader(
                new InputStreamReader(e.getContent(), 
                charset));
              String str = null;
              while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
              }
            }
          }
          if (response.getStatusLine().getStatusCode() >= 300) {
            throw new HttpResponseException(response.getStatusLine().getStatusCode(), 
              response.getStatusLine().getReasonPhrase());
          }
          if (e == null) {
            throw new ClientProtocolException("返回内容为空");
          }
          logger.info(response.getStatusLine().getStatusCode() + "\n" + buffer.toString());

          return buffer.toString();
        } catch (Exception e) {
        }
        finally {
          if (bufferedReader != null) {
            try {
              bufferedReader.close();
            } catch (IOException e) {
              e.printStackTrace();
              throw new RuntimeException(e);
            }
          }
//          httpGet.releaseConnection();
        }
        return null;
      }

      public static JSONObject doPost(String url, HttpEntity entity, String charset)
      {
        HttpClient hc = getHttpClient();
        HttpPost httpPost = new HttpPost(url);

        BufferedReader bufferedReader = null;
        try {
          httpPost.setEntity(entity);

          HttpResponse response = hc.execute(httpPost);
          HttpEntity e = response.getEntity();
          StringBuffer buffer = new StringBuffer();
          if (e != null) {
            Long len = Long.valueOf(e.getContentLength());
            if ((len.longValue() != -1L) && (len.longValue() < 2048L)) {
              buffer.append(EntityUtils.toString(e, charset));
            } else {
              bufferedReader = new BufferedReader(
                new InputStreamReader(e.getContent(), charset));
              String str = null;
              while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
              }
            }
          }
          if (response.getStatusLine().getStatusCode() >= 300) {
            throw new HttpResponseException(
              response.getStatusLine().getStatusCode(), 
              response.getStatusLine().getReasonPhrase());
          }

          if (e == null) {
            throw new ClientProtocolException("返回内容为空");
          }

          logger.info(response.getStatusLine().getStatusCode() + "\n" + 
            buffer.toString());
          return new JSONObject(buffer.toString());
        } catch (Exception e) {
        }
        finally {
          if (bufferedReader != null) {
            try {
              bufferedReader.close();
            } catch (IOException e) {
              e.printStackTrace();
              throw new RuntimeException(e);
            }
          }
//          httpPost.releaseConnection();
        }
        return null;
      }

      private static class ConnectionManager
      {
        private static PoolingHttpClientConnectionManager manager = null;

        static {
          SSLContext sslContext = null;
          try {
            sslContext = SSLContexts.custom().loadTrustMaterial(
              null, new TrustStrategy()
            {
              public boolean isTrusted(X509Certificate[] arg0, String arg1)
                throws CertificateException
              {
                return true;
              }
            }).build();
          } catch (KeyManagementException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
          } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
          } catch (KeyStoreException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
          }
          SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
            sslContext, 
            new String[] { "TLSv1" }, 
            null, 
            SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
          Registry registry = RegistryBuilder.create().register(
            "http", PlainConnectionSocketFactory.INSTANCE)
            .register("https", sslsf).build();
          manager = new PoolingHttpClientConnectionManager(registry);
          manager.setDefaultMaxPerRoute(20);
        }
      }

}
————————
package com.kingdee.eas.custom.cfinv.utils;

import com.kingdee.bos.ui.face.CoreUIObject;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

public class HttpUtils {

      private static Logger logger = CoreUIObject.getLogger(HttpUtils.class);

      public static HttpClient getHttpClient()
      {
        CloseableHttpClient httpClient = HttpClients.custom()
          .setConnectionManager(ConnectionManager.manager).build();
        return httpClient;
      }

      public static JSONObject doPost(String url, String params, String charset) throws Exception
      {
        logger.error("HttpUtils:start-----doPost");
        HttpClient hc = getHttpClient();
        HttpPost httpPost = new HttpPost(url);

        BufferedReader bufferedReader = null;
        try {
          logger.error("获取到response之前");
          StringEntity se = new StringEntity(params, charset);
          se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
          httpPost.setEntity(se);

          HttpResponse response = hc.execute(httpPost);
          HttpEntity e = response.getEntity();
          StringBuffer buffer = new StringBuffer();
          logger.error("获取到response之后");
          if (e != null) {
            Long len = Long.valueOf(e.getContentLength());
            if ((len.longValue() != -1L) && (len.longValue() < 2048L)) {
                logger.error("2048L");
                logger.error("2048L"+buffer.toString());
              buffer.append(EntityUtils.toString(e, charset));
            } else {
                logger.error("追加"+buffer.toString());
              bufferedReader = new BufferedReader(
                new InputStreamReader(e.getContent(), charset));
              String str = null;
              while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
              }
            }
          }
            logger.error("报文:"+buffer.toString());
          if (response.getStatusLine().getStatusCode() >= 300) {
              logger.error("code大于300:"+buffer.toString());
            throw new HttpResponseException(
              response.getStatusLine().getStatusCode(), 
              response.getStatusLine().getReasonPhrase());
          }

          if (e == null) {
              logger.error("返回内容为空");
            throw new ClientProtocolException("返回内容为空");
          }

          logger.error("HttpUtils.doPost=\r\n"+response.getStatusLine().getStatusCode() + "\n" + 
            buffer.toString());
        
          JSONObject resultObject = new JSONObject(buffer.toString());
          logger.error("HttpUtils.resultObject=\r\n"+resultObject);
          return resultObject;
        }
        catch (Exception e) {
            logger.error("doPost错误:"+e.getMessage(), e);
            throw new Exception(e);
        }
        finally {
          if (bufferedReader != null) {
            try {
              bufferedReader.close();
            } catch (IOException e) {
              e.printStackTrace();
              logger.error("dopost错误:"+e,e);
              throw new RuntimeException(e);
            }
          }
          logger.error("dopost关闭");
//          httpPost.releaseConnection();
        }
      }

      public static byte[] downloadFile(String url) {
          logger.error("HttpUtils:start-----downloadFile");
        HttpClient hc = getHttpClient();
        HttpGet httpGet = new HttpGet(url);
        InputStream in = null;
        byte[] b = (byte[])null;
        try {
          HttpResponse response = hc.execute(httpGet);
          HttpEntity e = response.getEntity();
          if (response.getStatusLine().getStatusCode() >= 300) {
            throw new HttpResponseException(
              response.getStatusLine().getStatusCode(), 
              response.getStatusLine().getReasonPhrase());
          }
          if (e != null) {
            in = e.getContent();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            int l = 0;
            byte[] tmp = new byte[1024];
            while ((l = in.read(tmp)) != -1) {
              out.write(tmp, 0, l);
            }
            b = out.toByteArray();
            out.close();
          } else {
            throw new ClientProtocolException("返回内容为空");
          }
          return b;
        }
      
        catch (Exception e) {
        }
        finally {
          if (in != null) {
            try {
              in.close();
            } catch (IOException e) {
              e.printStackTrace();
              logger.error("downloadFile错误:"+e,e);
              throw new RuntimeException(e);
            }
          }
//          httpGet.releaseConnection();
        }
        return null;
      }

      public static String encode(String[] args) {
        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        if ((args == null) || (args.length == 0)) return "";
        StringBuffer sb = new StringBuffer();
        for (String str : args) {
          sb.append(str);
        }
        byte[] bytes = sb.toString().getBytes();
        try {
          MessageDigest mdInst = MessageDigest.getInstance("MD5");
          mdInst.update(bytes);
          byte[] md = mdInst.digest();
          char[] ciphertext = new char[2 * md.length];
          int k = 0;
          for (int j = 0; j < md.length; ++j) {
            byte b0 = md[j];
            ciphertext[(k++)] = hexDigits[(b0 >>> 4 & 0xF)];
            ciphertext[(k++)] = hexDigits[(b0 & 0xF)];
          }
          return new String(ciphertext);
        } catch (NoSuchAlgorithmException e) {
          e.printStackTrace();
          logger.error("encode错误:"+e,e);
          throw new RuntimeException(e);
        }
      }

      public static JSONObject doGet(String url, String charset)
      {
          logger.error("HttpUtils:start-----doGet");
        HttpClient hc = getHttpClient();
        HttpGet httpGet = new HttpGet(url);
        BufferedReader bufferedReader = null;
        try {
          HttpResponse response = hc.execute(httpGet);
          HttpEntity e = response.getEntity();
          StringBuffer buffer = new StringBuffer();
          if (e != null) {
            Long len = Long.valueOf(e.getContentLength());
            if ((len.longValue() != -1L) && (len.longValue() < 2048L)) {
              buffer.append(EntityUtils.toString(e, charset));
            } else {
              bufferedReader = new BufferedReader(
                new InputStreamReader(e.getContent(), 
                charset));
              String str = null;
              while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
              }
            }
          }
          if (response.getStatusLine().getStatusCode() >= 300) {
            throw new HttpResponseException(response.getStatusLine().getStatusCode(), 
              response.getStatusLine().getReasonPhrase());
          }
          if (e == null) {
            throw new ClientProtocolException("返回内容为空");
          }
          logger.error("doGet报文状态码"+response.getStatusLine().getStatusCode() + "\n" + buffer.toString());
          logger.info(response.getStatusLine().getStatusCode() + "\n" + buffer.toString());

          return new JSONObject(buffer.toString());
        }
        catch (UnsupportedEncodingException e1)
        {
            logger.error("doGet错误:"+e1,e1);
        }
        catch (ClientProtocolException e)
        {
            logger.error("doGet错误:"+e,e);
        }
        catch (IOException e)
        {
            logger.error("doGet错误:"+e,e);
        }
        catch (JSONException e) {
            logger.error("doGet错误:"+e,e);
        }
        finally {
          if (bufferedReader != null) {
            try {
              bufferedReader.close();
            } catch (IOException e) {
              e.printStackTrace();
              logger.error("doGet错误:"+e,e);
              throw new RuntimeException(e);
            }
          }
//          httpGet.releaseConnection();
        }
        return null;
      }

      public static JSONObject doPost(String url, List<NameValuePair> params, String charset)
      {
        HttpClient hc = getHttpClient();
        HttpPost httpPost = new HttpPost(url);

        BufferedReader bufferedReader = null;
        try {
          StringEntity se = new UrlEncodedFormEntity(params, "UTF-8");
          se.setContentEncoding(new BasicHeader("Content-Type", "application/x-www-form-urlencoded"));
          httpPost.setEntity(se);

          HttpResponse response = hc.execute(httpPost);
          HttpEntity e = response.getEntity();
          StringBuffer buffer = new StringBuffer();
          if (e != null) {
            Long len = Long.valueOf(e.getContentLength());
            if ((len.longValue() != -1L) && (len.longValue() < 2048L)) {
              buffer.append(EntityUtils.toString(e, charset));
            } else {
              bufferedReader = new BufferedReader(
                new InputStreamReader(e.getContent(), charset));
              String str = null;
              while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
              }
            }
          }
          if (response.getStatusLine().getStatusCode() >= 300) {
            throw new HttpResponseException(
              response.getStatusLine().getStatusCode(), 
              response.getStatusLine().getReasonPhrase());
          }

          if (e == null) {
            throw new ClientProtocolException("返回内容为空");
          }

          logger.info(response.getStatusLine().getStatusCode() + "\n" + 
            buffer.toString());
          return new JSONObject(buffer.toString());
        } catch (Exception e) {
        }
        finally {
          if (bufferedReader != null) {
            try {
              bufferedReader.close();
            } catch (IOException e) {
              e.printStackTrace();
              throw new RuntimeException(e);
            }
          }
//          httpPost.releaseConnection();
        }
        return null;
      }

      public static String doGet4String(String url, String charset)
      {
        HttpClient hc = getHttpClient();
        HttpGet httpGet = new HttpGet(url);
        BufferedReader bufferedReader = null;
        try {
          HttpResponse response = hc.execute(httpGet);
          HttpEntity e = response.getEntity();
          StringBuffer buffer = new StringBuffer();
          if (e != null) {
            Long len = Long.valueOf(e.getContentLength());
            if ((len.longValue() != -1L) && (len.longValue() < 2048L)) {
              buffer.append(EntityUtils.toString(e, charset));
            } else {
              bufferedReader = new BufferedReader(
                new InputStreamReader(e.getContent(), 
                charset));
              String str = null;
              while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
              }
            }
          }
          if (response.getStatusLine().getStatusCode() >= 300) {
            throw new HttpResponseException(response.getStatusLine().getStatusCode(), 
              response.getStatusLine().getReasonPhrase());
          }
          if (e == null) {
            throw new ClientProtocolException("返回内容为空");
          }
          logger.info(response.getStatusLine().getStatusCode() + "\n" + buffer.toString());

          return buffer.toString();
        } catch (Exception e) {
        }
        finally {
          if (bufferedReader != null) {
            try {
              bufferedReader.close();
            } catch (IOException e) {
              e.printStackTrace();
              throw new RuntimeException(e);
            }
          }
//          httpGet.releaseConnection();
        }
        return null;
      }

      public static JSONObject doPost(String url, HttpEntity entity, String charset)
      {
        HttpClient hc = getHttpClient();
        HttpPost httpPost = new HttpPost(url);

        BufferedReader bufferedReader = null;
        try {
          httpPost.setEntity(entity);

          HttpResponse response = hc.execute(httpPost);
          HttpEntity e = response.getEntity();
          StringBuffer buffer = new StringBuffer();
          if (e != null) {
            Long len = Long.valueOf(e.getContentLength());
            if ((len.longValue() != -1L) && (len.longValue() < 2048L)) {
              buffer.append(EntityUtils.toString(e, charset));
            } else {
              bufferedReader = new BufferedReader(
                new InputStreamReader(e.getContent(), charset));
              String str = null;
              while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
              }
            }
          }
          if (response.getStatusLine().getStatusCode() >= 300) {
            throw new HttpResponseException(
              response.getStatusLine().getStatusCode(), 
              response.getStatusLine().getReasonPhrase());
          }

          if (e == null) {
            throw new ClientProtocolException("返回内容为空");
          }

          logger.info(response.getStatusLine().getStatusCode() + "\n" + 
            buffer.toString());
          return new JSONObject(buffer.toString());
        } catch (Exception e) {
        }
        finally {
          if (bufferedReader != null) {
            try {
              bufferedReader.close();
            } catch (IOException e) {
              e.printStackTrace();
              throw new RuntimeException(e);
            }
          }
//          httpPost.releaseConnection();
        }
        return null;
      }

      private static class ConnectionManager
      {
        private static PoolingHttpClientConnectionManager manager = null;

        static {
          SSLContext sslContext = null;
          try {
            sslContext = SSLContexts.custom().loadTrustMaterial(
              null, new TrustStrategy()
            {
              public boolean isTrusted(X509Certificate[] arg0, String arg1)
                throws CertificateException
              {
                return true;
              }
            }).build();
          } catch (KeyManagementException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
          } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
          } catch (KeyStoreException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
          }
          SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
            sslContext, 
            new String[] { "TLSv1" }, 
            null, 
            SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
          Registry registry = RegistryBuilder.create().register(
            "http", PlainConnectionSocketFactory.INSTANCE)
            .register("https", sslsf).build();
          manager = new PoolingHttpClientConnectionManager(registry);
          manager.setDefaultMaxPerRoute(20);
        }
      }

}