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.ltesting.net