HttpClient工具类

import java.io.IOException;

import java.io.InterruptedIOException;

import java.io.UnsupportedEncodingException;

import java.net.UnknownHostException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Random;

import java.util.Set;

import java.util.concurrent.CountDownLatch;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

 

import javax.net.ssl.SSLException;

import javax.net.ssl.SSLHandshakeException;

 

import org.apache.http.HttpEntity;

import org.apache.http.HttpEntityEnclosingRequest;

import org.apache.http.HttpHost;

import org.apache.http.HttpRequest;

import org.apache.http.NameValuePair;

import org.apache.http.NoHttpResponseException;

import org.apache.http.client.HttpRequestRetryHandler;

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.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.client.methods.HttpRequestBase;

import org.apache.http.client.protocol.HttpClientContext;

import org.apache.http.config.Registry;

import org.apache.http.config.RegistryBuilder;

import org.apache.http.conn.ConnectTimeoutException;

import org.apache.http.conn.routing.HttpRoute;

import org.apache.http.conn.socket.ConnectionSocketFactory;

import org.apache.http.conn.socket.LayeredConnectionSocketFactory;

import org.apache.http.conn.socket.PlainConnectionSocketFactory;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.protocol.HttpContext;

import org.apache.http.util.EntityUtils;

import org.apache.log4j.Logger;

 

/**

 * HttpClient工具类

 *

 * @return

 * @author SHANHY

 * @create 2015年12月18日

 */

public class HttpClientUtil

{

    static final int timeOut = 10 * 1000;

    private static CloseableHttpClient httpClient1 = null;

    private static CloseableHttpClient httpClient2 = null;

    private static CloseableHttpClient httpClient3 = null;

    private final static Object syncLock = new Object();

    private static final Logger LOG = Logger.getLogger(HttpClientUtil.class);

    private static void config(HttpRequestBase httpRequestBase)

    {

        // 设置Header等

//         httpRequestBase.setHeader("User-Agent", "Mozilla/5.0");

//         httpRequestBase

//         .setHeader("Accept",

//         "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

//         httpRequestBase.setHeader("Accept-Language",

//        "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");// "en-US,en;q=0.5");

//         httpRequestBase.setHeader("Accept-Charset",

//         "ISO-8859-1,utf-8,gbk,gb2312;q=0.7,*;q=0.7");

        

     

//       httpRequestBase.setHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*");

     

       httpRequestBase.setHeader("Connection", "Keep-alive");

         httpRequestBase.setHeader("Accept-Encoding", "gzip,deflate");

         httpRequestBase.setHeader("User-Agent", "Apache-HttpClient/4.5.2 (Java/1.8.0_73)");

         httpRequestBase.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

         httpRequestBase.setHeader("Host", "47.92.116.172");

         httpRequestBase.setHeader("Content-Length", "8");

//         httpRequestBase.setHeader("Pragma", "no-cache");

        

//         配置请求的超时设置

        RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeOut).setConnectTimeout(timeOut).setSocketTimeout(timeOut).build();

        httpRequestBase.setConfig(requestConfig);

    }

  

    /**

     * 获取HttpClient1对象

     *

     * @return

     * @author SHANHY

     * @create 2015年12月18日

     */

    public static CloseableHttpClient getHttpClient1(String url)

    {

       // LOG.info("---CloseableHttpClient:---"+url);

      String hostname = url.split("/")[2];

      //LOG.info("---CloseableHttpClient:---"+hostname);

        int port = 80;

        if (hostname.contains(":"))

        {

            String[] arr = hostname.split(":");

            hostname = arr[0];

            port = Integer.parseInt(arr[1]);

        }

        if (httpClient1 == null)

        {

            synchronized (syncLock)

            {

                if (httpClient1 == null)

                {

                    httpClient1 = createHttpClient(200, 40, 100, hostname, port);

                }

            }

        }

        return httpClient1;

    }

    /**

     * 获取HttpClient对象

     *

     * @return

     * @author SHANHY

     * @create 2015年12月18日

     */

    public static CloseableHttpClient getHttpClient2(String url)

    {

        String hostname = url.split("/")[2];

        int port = 80;

        if (hostname.contains(":"))

        {

            String[] arr = hostname.split(":");

            hostname = arr[0];

            port = Integer.parseInt(arr[1]);

        }

        if (httpClient2 == null)

        {

            synchronized (syncLock)

            {

                if (httpClient2 == null)

                {

                    httpClient2 = createHttpClient(200, 40, 100, hostname, port);

                }

            }

        }

        return httpClient2;

    }

    /**

     * 获取HttpClient对象

     *

     * @return

     * @author SHANHY

     * @create 2015年12月18日

     */

    public static CloseableHttpClient getHttpClient3(String url)

    {

        String hostname = url.split("/")[2];

        int port = 80;

        if (hostname.contains(":"))

        {

            String[] arr = hostname.split(":");

            hostname = arr[0];

            port = Integer.parseInt(arr[1]);

        }

        if (httpClient3 == null)

        {

            synchronized (syncLock)

            {

                if (httpClient3 == null)

                {

                    httpClient3 = createHttpClient(200, 40, 100, hostname, port);

                }

            }

        }

        return httpClient3;

    }   

    /**

     *

     * @param maxTotal    最大连接数

     * @param maxPerRoute 每个路由基础的连接

     * @param maxRoute    目标主机的最大连接数

     * @param hostname    连接地址

     * @param port        连接端口

     * @return

     */

    public static CloseableHttpClient createHttpClient(int maxTotal,int maxPerRoute, int maxRoute, String hostname, int port)

    {

        ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();

        LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();

        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create().register("http", plainsf).register("https", sslsf).build();

        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);

        // 将最大连接数增加

        cm.setMaxTotal(maxTotal);

        // 将每个路由基础的连接增加

        cm.setDefaultMaxPerRoute(maxPerRoute);

        HttpHost httpHost = new HttpHost(hostname, port);

        // 将目标主机的最大连接数增加

        cm.setMaxPerRoute(new HttpRoute(httpHost), maxRoute);

        // 请求重试处理

        HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler()

        {

            public boolean retryRequest(IOException exception,int executionCount, HttpContext context)

            {

                if (executionCount >= 5)

                {// 如果已经重试了5次,就放弃

                    return false;

                }

                if (exception instanceof NoHttpResponseException)

                {// 如果服务器丢掉了连接,那么就重试

                    return true;

                }

                if (exception instanceof SSLHandshakeException)

                {// 不要重试SSL握手异常

                    return false;

                }

                if (exception instanceof InterruptedIOException)

                {// 超时

                    return false;

                }

                if (exception instanceof UnknownHostException)

                {// 目标服务器不可达

                    return false;

                }

                if (exception instanceof ConnectTimeoutException)

                {// 连接被拒绝

                    return false;

                }

                if (exception instanceof SSLException)

                {// SSL握手异常

                    return false;

                }

                HttpClientContext clientContext = HttpClientContext.adapt(context);

                HttpRequest request = clientContext.getRequest();

                // 如果请求是幂等的,就再次尝试

                if (!(request instanceof HttpEntityEnclosingRequest))

                {

                    return true;

                }

                return false;

            }

        };

        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).setRetryHandler(httpRequestRetryHandler).build();

        return httpClient;

    }

 

    private static void setPostParams(HttpPost httpost, Map<String, Object> params)

    {

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();

        Set<String> keySet = params.keySet();

        for (String key : keySet)

        {

            nvps.add(new BasicNameValuePair(key, params.get(key).toString()));

        }

        try

        {

            httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));

        } catch (UnsupportedEncodingException e)

        {

            e.printStackTrace();

        }

    }

 

   /**

    *  POST请求URL获取内容

    * @param url

    * @param params

    * @return

    * @throws IOException

    */

    public static String post(String url, Map<String, Object> params,int i) throws IOException

    {

        HttpPost httppost = new HttpPost(url);

        config(httppost);

        setPostParams(httppost, params);

        CloseableHttpResponse response = null;

        try

        {

            if(i==0||i==1)

            {

            response = getHttpClient1(url).execute(httppost, HttpClientContext.create());

            }else if(i==2)

            {

            response = getHttpClient2(url).execute(httppost, HttpClientContext.create());

            }else

            {

            response = getHttpClient3(url).execute(httppost, HttpClientContext.create());

            }

            HttpEntity entity = response.getEntity();

            String result = EntityUtils.toString(entity, "utf-8");

            EntityUtils.consume(entity);

            return result;

        } catch (Exception e)

        {

        e.printStackTrace();

        return null;

        } finally

        {

            try

            {

                if (response != null)

                    response.close();

            } catch (IOException e)

            {

                e.printStackTrace();

            }

        }

    }

 

    /**

     * GET请求URL获取内容

     *

     * @param url

     * @return

     * @author SHANHY

     * @create 2015年12月18日

     */

    public static String get(String url,int i) {

        HttpGet httpget = new HttpGet(url);

        config(httpget);

        CloseableHttpResponse response = null;

        try

        {

          

            if(i==0||i==1)

            {

            response = getHttpClient1(url).execute(httpget,HttpClientContext.create());

            }else if(i==2)

            {

            response = getHttpClient2(url).execute(httpget,HttpClientContext.create());

            }else

            {

            response = getHttpClient3(url).execute(httpget,HttpClientContext.create());

            }        

       

       

            HttpEntity entity = response.getEntity();

            String result = EntityUtils.toString(entity, "utf-8");

            EntityUtils.consume(entity);

            return result;

        } catch (IOException e)

        {

            e.printStackTrace();

        } finally

        {

            try

            {

                if (response != null)

                {

                    response.close();

                }

            } catch (IOException e)

            {

                e.printStackTrace();

            }

        }

        return null;

    }

 

    public static void main(String[] args)

    {

      Map params=new HashMap();

      long zongjis=0;

      for(int i=0;i!=10000;i++)

      {

         params.put("param", "Mr(FFF)NaoNE7mZ(DDD)q4LutbtuBEYjvno4mdgF21edkDUDeJOFLaj8u(DDD)2ZAylS6I7Y TLriLpRY9O3Ico1zaluE2OnwxQ6SwimZ71hLzyRKTi(DDD)uFNlYLi(FFF)L5fTVdOro Ji9YVOah44Oeuq9maSEjZcRod4imZG6fA9XEegAZ8bHodd4DSnYpGsPl(DDD)wRf bjHJgCSP88xYjOJGfwyyn7P1ZZWP7QI4q6WdmETFlYpPqQrRbpLPWHJKmrb5 ZoUWiLt5K90ATCzGLURxHcn027htQlFlC3LDqYyeyER3Fw6TuKr(DDD)c0DIY3cV Ux0bKMGseEU9opM1EGOjzdULWWfrVctY70Kkt2HhBxdLAJZeVKuGvAWZsckg rv7qxqZfdayyj0bs2mEcZAujrVKUu(FFF)0SMbIsiYglMAfGK97nZznX6Kzhl1Gs IdQLRwGs8HwhdGaDGUaSs59oN9KyfMdR4c(FFF)stkRBrTqJ6QJauGNs2Y2Yc(FFF)q1 d2b9kGTQOEeYGzQhGbE1vI4UdXT(DDD)xLEPUQQ27TGXjuyaNiLf5LZ8FcYycJAM gMD(FFF)vY9p6DIC(FFF)(DDD)tqw3Dd0z51GHxc6OoEn4SgSs7NLdu(FFF)mag25EIa(FFF)vXqoitY fhf6h(FFF)(FFF)9DtJJBoCn116eEQDlHvt29gqqxyt(DDD)YxO11O2PfKnkVndKJQuHEiCk 3q4XtmKNWoXPEOgUlogO(FFF)0(FFF)0EawW(FFF)PwDf3mZ3Ez5wrQZ");

         params.put("orderid", "1484105435532");

         params.put("transcode", "800001");

         params.put("mchid", "10041");

         params.put("signature", "B892B1B6D62F8CD31B92BE835A1596B1");

         Random rand = new Random();

         int randNum = rand.nextInt(3);

          

         try

         {

           

            long start = System.currentTimeMillis();

            String url="http://192.168.1.108:5080"+(randNum+1)+"/bginter/100bingo.do";

            System.out.println("===============================================url"+url+"=====");

            String   res=HttpClientUtil.post(url, params,(randNum+1));

           long end = System.currentTimeMillis();

           zongjis= zongjis+(end - start);

            System.out.println("===============================================总耗时"+zongjis+"毫秒"+res);

            System.out.println("===================================================第"+i+"条记录"+res);         

           if(i!=0){

              System.out.println("=============================================用时平均 " + zongjis/i);

           }else

           {

              System.out.println("=============================================用时平均 " + zongjis);

           }

         } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

        }

        

      }

 

    }

 

  

}

 

posted on 2019-02-19 13:23  我是司  阅读(730)  评论(0编辑  收藏  举报

导航