• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • HttpComponents組件特性探究(6)

    發表于:2012-11-23來源:Csdn作者:fengjia10點擊數: 標簽:開源框架
    .getHttpConnectionManager().getParams(); httpConnectionManagerParams.setConnectionTimeout(connectionTimeout); httpConnectionManagerParams.setTcpNoDelay(true);//Nagles algorithm httpConnectionManagerPa

      .getHttpConnectionManager().getParams();

      httpConnectionManagerParams.setConnectionTimeout(connectionTimeout);

      httpConnectionManagerParams.setTcpNoDelay(true);//Nagle's algorithm

      httpConnectionManagerParams.setSoTimeout(timeOut);

      httpConnectionManagerParams.setDefaultMaxConnectionsPerHost(maxHostConnections);

      httpConnectionManagerParams.setMaxTotalConnections(maxTotalConnections);

      //3.

      HttpClientParams httpClientParam = httpClient.getParams();

      //httpClientParam.setConnectionManagerTimeout(connectionTimeout);//暫且不關注這個超時設置,后面根據性能酌情考慮

      httpClientParam.setParameter(HttpMethodParams.RETRY_HANDLER,

      new DefaultHttpMethodRetryHandler(retryCount, false));

      httpClientParam.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

      return httpClient;

      }

      public static JSONObject doGet(String url, NameValuePair[] params) {

      return executeMethod(createHttpClient(), createGetMethod(url, params));

      }

      public static JSONObject doPost(String url, NameValuePair[] params) {

      return executeMethod(createHttpClient(), createPostMethod(url, params));

      }

      protected HttpClientUtils() {

      }

      public void setTimeOut(int timeOut) {

      HttpClientUtils.timeOut = timeOut;

      }

      public static int getTimeOut() {

      return timeOut;

      }

      public static int getRetryCount() {

      return retryCount;

      }

      public void setRetryCount(int retryCount) {

      HttpClientUtils.retryCount = retryCount;

      }

      public static int getConnectionTimeout() {

      return connectionTimeout;

      }

      public void setConnectionTimeout(int connectionTimeout) {

      HttpClientUtils.connectionTimeout = connectionTimeout;

      }

      public static int getMaxHostConnections() {

      return maxHostConnections;

      }

      public void setMaxHostConnections(int maxHostConnections) {

      HttpClientUtils.maxHostConnections = maxHostConnections;

      }

      public static int getMaxTotalConnections() {

      return maxTotalConnections;

      }

      public void setMaxTotalConnections(int maxTotalConnections) {

      HttpClientUtils.maxTotalConnections = maxTotalConnections;

      }

      public static String getCharsetName() {

      return charsetName;

      }

      public void setCharsetName(String charsetName) {

      HttpClientUtils.charsetName = charsetName;

      }

      }

      /**

      * @author von gosling 2011-12-12

      */

      public class HttpClientUtils {

      private static final Logger log = LoggerFactory

      .getLogger(HttpClientUtils.class);

      private static int timeOut = 100;

      private static int retryCount = 1;

      private static int connectionTimeout = 100;

      private static int maxHostConnections = 32; //根據apache work MPM設置此值

      private static int maxTotalConnections = 512; //同上

      private static String charsetName = "UTF-8";

      public static JSONObject executeMethod(HttpClient httpClient, HttpMethod method) {

      JSONObject result = new JSONObject();

      StopWatch watch = new StopWatch();

      int status = -1;

      try {

      log.info("Execute method({}) begin...", method.getURI());

      watch.start();

      status = httpClient.executeMethod(method);

      watch.stop();

      if (status == HttpStatus.SC_OK) {

      InputStream inputStream = method.getResponseBodyAsStream();

      ByteArrayOutputStream baos = new ByteArrayOutputStream();

      IOUtils.copy(inputStream, baos);

      String response = new String(baos.toByteArray(), charsetName);

      log.info("Response is:{}", response);

      result = JSONObject.parseObject(response);

      } else {

      log.error("Http request failure! status is {}", status);

      }

      } catch (SocketTimeoutException e) {

      log.error("Request time out!");//只關注請求超時,對于其它兩類超時,使用通用異常捕獲

      } catch (Exception e) {

      log.error("Error occur!", e);

      } finally {

      method.releaseConnection();

      log.info("Method {},statusCode {},consuming {} ms", new Object[] { method.getName(),

      status, watch.getTime() });

      }

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>