• <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組件特性探究(7)

    發表于:2012-11-23來源:Csdn作者:fengjia10點擊數: 標簽:開源框架
    return result; } /** * @param uri * @param nameValuePairs * @return */ public static PostMethod createPostMethod(String uri, NameValuePair[] nameValuePairs) { PostMethod method = new PostMethod(uri);

      return result;

      }

      /**

      * @param uri

      * @param nameValuePairs

      * @return

      */

      public static PostMethod createPostMethod(String uri, NameValuePair[] nameValuePairs) {

      PostMethod method = new PostMethod(uri);

      method.addParameters(nameValuePairs);

      method.getParams().setContentCharset(charsetName);

      return method;

      }

      /**

      * @param uri

      * @param nameValuePairs

      * @return

      */

      public static GetMethod createGetMethod(String uri, NameValuePair[] nameValuePairs) {

      GetMethod method = new GetMethod(uri);

      List list = Lists.newArrayList();

      if (nameValuePairs != null) {

      Collections.addAll(list, nameValuePairs);

      method.setQueryString(list.toArray(new NameValuePair[nameValuePairs.length]));

      }

      method.getParams().setContentCharset(charsetName);

      return method;

      }

      public static HttpClient createHttpClient() {

      //1.

      HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());

      //2.

      HttpConnectionManagerParams httpConnectionManagerParams = httpClient

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

      }

      }

      好了,有了活生生的代碼,我們來總結一下httpClient封裝過程中需要注意的一些事項吧。恩,其實更多的是體現在安全,性能上面:

      (1)多線程模型,尤其注意finally中collection的釋放問題。除此之外,需要考慮池化連接的異常處理,這是我文中提到特別注意的三大異常之一;

      (2)Retry機制中對冪等性的處理。尤其是在httpClient4中,put和post操作,未按照http規范行事,需要我們額外注意;

      (3)SSL、TLS的定制化處理;

      (4)并發標記的處理,這里使用了Concurrency in practice中的并發annotation,有什么用?感興趣的朋友可以了解下SureLogic(http://www.surelogic.com/concurrency-tools.html),別問我要license,因為俺也不是apache開源社區的developer呀;

      (5)攔截器對header的處理;

    原文轉自: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>