@Test
  public void testConnectionResetByHttpClientUtils() throws IOException {
    testConnectionResetCount = 0;

    httpClientUtils = new HttpClientUtils();
    httpClientUtils.initHttpClient();
    Logger.info(this, "-------------- HttpClient initialized -------------");

    String resp = null;
    try {
      resp = httpClientUtils.get("http://localhost:65532/soso");
    } catch (IOException e) {
      Throwable ee = ExceptionUtils.getRootCause(e);
      if (ee == null) {
        ee = e;
      }
      Logger.error(this, "", ee);
      Assert.assertNotSame(NoHttpResponseException.class, ee.getClass());
      Assert.assertSame(SocketException.class, ee.getClass());
      Assert.assertTrue(
          "Connection reset".equals(ee.getMessage())
              || "Socket closed".equals(ee.getMessage())
              || "Unexpected end of file from server".equals(ee.getMessage()));
    } finally {
      Logger.info(
          this,
          "resp[HttpURLConnection]-["
              + testConnectionResetCount
              + "]=========["
              + resp
              + "]=========");
    }
    Assert.assertEquals(1, testConnectionResetCount);
  }
  @Test
  public void test_get_baidu() throws Exception {
    httpClientUtils = new HttpClientUtils();
    httpClientUtils.setReUserConnection(false);
    httpClientUtils.initHttpClient();
    Logger.info(this, "-------------- HttpClient initialized -------------");
    //        for(int i =0;i<3){
    //
    //        }
    httpClientUtils.get("http://www.lufax.com" + "?" + RandomUtils.nextInt());
    httpClientUtils.get("http://www.lufax.com" + "?" + RandomUtils.nextInt());
    //         httpClientUtils.get("http://www.lufax.com" + "?" + RandomUtils.nextInt());
    //         httpClientUtils.get("http://www.lufax.com" + "?" + RandomUtils.nextInt());
    //         httpClientUtils.get("http://www.lufax.com" + "?" + RandomUtils.nextInt());
    //         httpClientUtils.get("http://www.lufax.com" + "?" + RandomUtils.nextInt());

    ExceptionHandler exceptionHandler =
        new ExceptionHandler() {
          public boolean handle(Throwable e) {
            return true;
          }
        };
    runTest("http://www.lufax.com", 1, exceptionHandler, 10);

    //        System.out.println(resp);
  }
 @Test
 public void testInjectedHttpClient() throws IOException {
   final DefaultHttpClient httpClient =
       new DefaultHttpClient(HttpClientUtils.createDefaultHttpParams());
   HttpClientUtils.enableCompression(httpClient);
   final SardineHttpClientImpl sardine = new SardineHttpClientImpl(httpClient);
   checkMultipleResources(toMap(sardine.list(SVN_BASE_URL)));
 }
  @Test
  public void testConnectionCount() {
    testConnectionCount = 0;
    httpClientUtils = new HttpClientUtils();
    //		httpClientUtils.setMaxConnPerRoute(0);  // default:-1, means 2
    //		httpClientUtils.setMaxConnTotal(0);     // default:-1, means 20

    httpClientUtils.setMaxConnPerRoute(3);
    httpClientUtils.setMaxConnTotal(100);

    //		httpClientUtils.setMaxConnPerRoute(100);
    //		httpClientUtils.setMaxConnTotal(3);

    httpClientUtils.initHttpClient();
    Logger.info(this, "-------------- HttpClient initialized -------------");

    final ExceptionHandler exceptionHandler =
        new ExceptionHandler() {
          public boolean handle(Throwable e) {
            return e == null;
          }
        };

    new Thread("t-1") {
      @Override
      public void run() {
        runTest("http://localhost:65534/test/testConnectionCount", 7, exceptionHandler, 2);
      }
    }.start();
    new Thread("t-2") {
      @Override
      public void run() {
        runTest("http://localhost:65533/test/testConnectionCount", 7, exceptionHandler, 1);
      }
    }.start();
    new Thread("t-3") {
      @Override
      public void run() {
        runTest("http://www.baidu.com", 1, exceptionHandler, 10);
      }
    }.start();
    runTest("https://www.lufax.com", 1, exceptionHandler, 3);
    /*
            测试结论:
    		1、 ”Connection“ == 【URL】;“Route” == 【IP:port】。
    		2、连接数越多,并发性能越高;反之,将频繁进行Connection released/leased。
    		3、当连接数满足不了并发的要求时,并发请求Connection的线程将wait。

    		【route allocated: 1 of 3; total allocated: 9 of 100】
    [DEBUG]-[2014-03-07 09:56:46.828]-[http://localhost:65533/test/testConnectionCount_runTest_1] - Connection released: [id: 4][route: {}->http://localhost:65533][total kept alive: 6; route allocated: 1 of 3; total allocated: 9 of 100]
    [org.apache.http.impl.conn.PoolingHttpClientConnectionManager]
    	org.apache.http.impl.conn.PoolingHttpClientConnectionManager.releaseConnection(PoolingHttpClientConnectionManager.java:282)
    		 */
  }
  /** @return */
  private DefaultHttpClient createHttpClient() {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpClientUtils.disableRetry(client);

    if (StringUtils.isNotBlank(proxy)) {
      HttpClientUtils.setProxy(client, proxy);
    }
    if (needAuth()) {
      HttpClientUtils.setAuth(client, authCritendial[0], authCritendial[1]);
    }
    return client;
  }
 public static void main(String[] args) throws IOException {
   String s = "http://app.zhcw.com/wwwroot/zhcw/jsp/MediaArena2/leitai.jsp?issueId=new&utilType=1";
   String p = "www-proxy.ericsson.se:8080";
   DefaultHttpClient httpclient = HttpClientUtils.createClient(10);
   HttpClientUtils.setProxy(httpclient, p);
   HttpGet method = new HttpGet(s);
   try {
     HttpResponse resp = httpclient.execute(method);
     System.out.println(resp.getEntity().getContent().toString());
   } catch (IOException e) {
     // cancel the connection when the error happen
     throw e;
   }
 }
  private HttpStatus commit(HttpUriRequest method) throws IOException {
    HttpStatus status;
    HttpClient httpclient = createHttpClient();
    // reduce the TIME_WAIT
    // method.addHeader("Connection", "close");

    if (method.getFirstHeader("Referer") == null) {
      URI uri = method.getURI();
      method.setHeader("Referer", uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort());
    }
    ;

    try {
      HttpResponse resp = execute(method, httpclient);

      status = new HttpStatus(resp.getStatusLine());
      if (resp.getEntity() != null) {
        status.setMessage(EntityUtils.toString(resp.getEntity(), "UTF-8"));
      }
    } catch (IOException e) {
      // cancel the connection when the error happen
      method.abort();
      throw e;
    } finally {
      HttpClientUtils.abortConnection(method, httpclient);
    }
    return status;
  }
 @After
 public void clean() {
   Logger.info(
       this,
       "============= test take time milliseconds: " + (System.currentTimeMillis() - beginTime));
   if (httpClientUtils != null) {
     httpClientUtils.destroy();
   }
 }
 @Before
 public void reset() {
   Logger.info(this, "-------------- reset begin -------------");
   beginTime = System.currentTimeMillis();
   if (httpClientUtils != null) {
     httpClientUtils.destroy();
   }
   Logger.info(this, "-------------- reset end -------------");
 }
  /**
   * @param method
   * @param httpclient
   * @return
   * @throws IOException
   * @throws ClientProtocolException
   */
  private HttpResponse execute(HttpUriRequest method, HttpClient httpclient)
      throws IOException, ClientProtocolException {
    if (needAuth()) {
      return HttpClientUtils.executeWithBasicAuth(method, httpclient);
    }

    if (soTimeout > 0) {
      HttpConnectionParams.setSoTimeout(method.getParams(), soTimeout);
    }

    return httpclient.execute(method);
  }
  @Test
  public void testSocketTimeout() {
    testConnectionTimeoutCount = 0;
    httpClientUtils = new HttpClientUtils();

    httpClientUtils.setMaxConnPerRoute(1);
    httpClientUtils.setMaxConnTotal(1);
    httpClientUtils.setSocketTimeout(2500);

    httpClientUtils.initHttpClient();
    Logger.info(this, "-------------- HttpClient initialized -------------");

    ExceptionHandler exceptionHandler =
        new ExceptionHandler() {
          public boolean handle(Throwable e) {
            return (SocketTimeoutException.class == e.getClass())
                && "Read timed out".equals(e.getMessage());
          }
        };

    runTest("http://localhost:65534/test/testConnectionTimeout", 1, exceptionHandler, 1);
  }
  @Test
  public void testNoHttpResponseExceptionRetry() {
    testConnectionResetCount = 0;
    httpClientUtils = new HttpClientUtils();
    httpClientUtils.initHttpClient();

    ExceptionHandler exceptionHandler =
        new ExceptionHandler() {
          public boolean handle(Throwable e) {
            return e instanceof NoHttpResponseException;
          }
        };
    runTest("http://localhost:65534/test/testNoHttpResponseException", 1, exceptionHandler, 1);
  }
 @Test
 public void testPostCustomerBodyAndCharset() {
   httpClientUtils = new HttpClientUtils();
   httpClientUtils.initHttpClient();
   Logger.info(this, "-------------- HttpClient initialized -------------");
   // testPostCustomerBodyAndCharset
   String resp = "";
   try {
     resp =
         httpClientUtils.post(
             "http://localhost:65534/test/testPostCustomerBodyAndCharset",
             "<?xml version=\"1.0\" encoding=\"GBK\"?><AIPG><INFO>你好</INFO></AIPG>",
             "GBK");
   } catch (IOException e) {
     Throwable ee = ExceptionUtils.getRootCause(e);
     if (ee == null) {
       ee = e;
     }
     Logger.error(this, "", ee);
     Assert.assertTrue(false);
     //            Assert.assertNotSame(NoHttpResponseException.class, ee.getClass());
     //            Assert.assertSame(SocketException.class, ee.getClass());
     //            Assert.assertTrue("Connection reset".equals(ee.getMessage()) || "Socket
     // closed".equals(ee.getMessage()) || "Unexpected end of file from
     // server".equals(ee.getMessage()));
   } finally {
     Logger.info(
         this,
         "resp[HttpURLConnection]-["
             + testConnectionResetCount
             + "]=========["
             + resp
             + "]=========");
   }
   //        Assert.assertEquals(1, testConnectionResetCount);
   Assert.assertTrue(true);
 }
  @Test
  public void testConnectionRequestTimeout() {
    testConnectionResetCount = 0;
    httpClientUtils = new HttpClientUtils();

    httpClientUtils.setMaxConnPerRoute(1);
    httpClientUtils.setMaxConnTotal(1);
    httpClientUtils.setConnectRequestTimeout(100);
    httpClientUtils.setConnectTimeout(10);

    httpClientUtils.initHttpClient();

    ExceptionHandler exceptionHandler =
        new ExceptionHandler() {
          public boolean handle(Throwable e) {
            if (e.getMessage().contains("Timeout waiting for connection from pool")) {
              Assert.assertTrue(false);
              // throw new RuntimeException("No available connect to user", e);
            }
            return e == null;
          }
        };
    runTest("http://localhost:65534/test/testConnectionRequestTimeout", 1, exceptionHandler, 2);
  }
  @Test
  public void testConnectionTimeout() {
    testConnectionTimeoutCount = 0;
    httpClientUtils = new HttpClientUtils();

    httpClientUtils.setMaxConnPerRoute(1);
    httpClientUtils.setMaxConnTotal(1);

    /*
          PING www.a.shifen.com (115.239.210.26) 56(84) bytes of data.
    64 bytes from 115.239.210.26: icmp_seq=1 ttl=50 time=6.97 ms
    PING www-wide.l.google.com (74.125.128.199) 56(84) bytes of data.
    64 bytes from hg-in-f199.1e100.net (74.125.128.199): icmp_seq=1 ttl=42 time=33.2 ms
     */
    httpClientUtils.setConnectTimeout(10);

    httpClientUtils.initHttpClient();
    Logger.info(this, "-------------- HttpClient initialized -------------");

    ExceptionHandler exceptionHandler =
        new ExceptionHandler() {
          public boolean handle(Throwable e) {
            return e == null;
          }
        };
    ExceptionHandler exceptionHandler2 =
        new ExceptionHandler() {
          public boolean handle(Throwable e) {
            return (ConnectException.class == e.getClass())
                && "Network is unreachable".equals(e.getMessage());
          }
        };

    runTest("http://www.baidu.com", 1, exceptionHandler, 1);
    runTest("https://www.lufax.com/", 1, exceptionHandler2, 1);

    /*
            测试结论:
    		1、...timed out. Connection will be retried using another IP address

    		9、runTest("https://www.google.com.hk", 1, exceptionHandler, 1); 测试日志阶段截段:
    [BUG]-[2014-03-05 19:40:06.211]-[testConnectionCount_12] - Connecting to www.google.com.hk/74.125.128.199:443
    [org.apache.http.conn.HttpClientConnectionManager]
    	org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:114)

    [DEBUG]-[2014-03-05 19:40:06.213]-[testConnectionCount_12] - Connect to www.google.com.hk/74.125.128.199:443 timed out. Connection will be retried using another IP address
    [org.apache.http.conn.HttpClientConnectionManager]
    	org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:143)

    [DEBUG]-[2014-03-05 19:40:06.214]-[testConnectionCount_12] - Connecting to www.google.com.hk/2404:6800:4005:c00:0:0:0:c7:443
    [org.apache.http.conn.HttpClientConnectionManager]
    	org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:114)

    [DEBUG]-[2014-03-05 19:40:06.214]-[testConnectionCount_12] - http-outgoing-101: Shutdown connection
    [org.apache.http.impl.conn.DefaultManagedHttpClientConnection]
    	org.apache.http.impl.conn.LoggingManagedHttpClientConnection.shutdown(LoggingManagedHttpClientConnection.java:87)

    [DEBUG]-[2014-03-05 19:40:06.214]-[testConnectionCount_12] - Connection discarded
    [org.apache.http.impl.execchain.MainClientExec]
    	org.apache.http.impl.execchain.ConnectionHolder.abortConnection(ConnectionHolder.java:126)

    [DEBUG]-[2014-03-05 19:40:06.214]-[testConnectionCount_12] - http-outgoing-101: Close connection
    [org.apache.http.impl.conn.DefaultManagedHttpClientConnection]
    	org.apache.http.impl.conn.LoggingManagedHttpClientConnection.close(LoggingManagedHttpClientConnection.java:79)

    [DEBUG]-[2014-03-05 19:40:06.214]-[testConnectionCount_12] - Connection released: [id: 101][route: {s}->https://www.google.com.hk:443][total kept alive: 0; route allocated: 0 of 1; total allocated: 0 of 1]
    [org.apache.http.impl.conn.PoolingHttpClientConnectionManager]
    	org.apache.http.impl.conn.PoolingHttpClientConnectionManager.releaseConnection(PoolingHttpClientConnectionManager.java:282)

    [DEBUG]-[2014-03-05 19:40:06.214]-[testConnectionCount_13] - Connection leased: [id: 102][route: {s}->https://www.google.com.hk:443][total kept alive: 0; route allocated: 1 of 1; total allocated: 1 of 1]
    [org.apache.http.impl.conn.PoolingHttpClientConnectionManager]
    	org.apache.http.impl.conn.PoolingHttpClientConnectionManager.leaseConnection(PoolingHttpClientConnectionManager.java:246)
    		 */
  }
 /**
  * Do a general post request with binary data .
  *
  * @param url
  * @param params
  * @param body
  * @return
  * @throws IOException any network exception happen
  */
 public HttpStatus doPost(String url, Map<String, String> params, byte[] body) throws IOException {
   return doPost(url, null, params, HttpClientUtils.createEntity(body));
 }
 public HttpStatus doPost(
     String url, Map<String, String> headers, Map<String, String> formParameters)
     throws IOException {
   return doPost(url, headers, null, HttpClientUtils.createEntity(formParameters));
 }