zoukankan      html  css  js  c++  java
  • java-commons-HttpClient超时设置setConnectionTimeout和setSoTimeout

    问题

    之前使用httpclient请求数据

    源码方法:

    复制代码
        public static String doHttp(HttpMethod result, int timeout, String charset) {
            HttpClient client = new HttpClient();
            try {
                HttpConnectionManagerParams managerParams = client.getHttpConnectionManager().getParams();
                managerParams.setConnectionTimeout(timeout);
                client.executeMethod(result);
                InputStream resStream = result.getResponseBodyAsStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(resStream, charset));
                StringBuffer resBuffer = new StringBuffer();
                String resTemp = "";
                while ((resTemp = br.readLine()) != null) {
                    resBuffer.append(resTemp);
                }
                return resBuffer.toString();
            } catch (HttpException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                result.releaseConnection();
            }
            return null;
        }
    复制代码

     

    发现其中在

    client.executeMethod(result);

     

      一只会卡在这里不出来,究其原因在于对

    managerParams.setConnectionTimeout(timeout);//为连接超时

     

    这里使用的是连接超时,而我这里还缺少了一个数据超时,否则会一直等待数据返回所以在下面在添加请求数据超时代码。

    以下为完整的代码:(其中红色部分为这次的主角)

    复制代码
        public static String doHttp(HttpMethod result, int timeout, String charset) {
            HttpClient client = new HttpClient();
            try {
                HttpConnectionManagerParams managerParams = client.getHttpConnectionManager().getParams();
                managerParams.setConnectionTimeout(timeout);
                managerParams.setSoTimeout(timeout);//等待结果超时
                client.executeMethod(result);
                InputStream resStream = result.getResponseBodyAsStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(resStream, charset));
                StringBuffer resBuffer = new StringBuffer();
                String resTemp = "";
                while ((resTemp = br.readLine()) != null) {
                    resBuffer.append(resTemp);
                }
                return resBuffer.toString();
            } catch (HttpException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                result.releaseConnection();
            }
            return null;
        }
    复制代码

     

     

     

    看介绍

     本文介绍来自http://jinnianshilongnian.iteye.com/blog/2089792

    参数设置
    1、httpclient 4.2.3
    复制代码
    HttpParams params = new BasicHttpParams();
    //设置连接超时时间
    Integer CONNECTION_TIMEOUT = 2 * 1000; //设置请求超时2秒钟 根据业务调整
    Integer SO_TIMEOUT = 2 * 1000; //设置等待数据超时时间2秒钟 根据业务调整
    //定义了当从ClientConnectionManager中检索ManagedClientConnection实例时使用的毫秒级的超时时间
    //这个参数期望得到一个java.lang.Long类型的值。如果这个参数没有被设置,默认等于CONNECTION_TIMEOUT,因此一定要设置
    Long CONN_MANAGER_TIMEOUT = 500L; //该值就是连接不够用的时候等待超时时间,一定要设置,而且不能太大 ()
     
    params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, SO_TIMEOUT);
    params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT, CONN_MANAGER_TIMEOUT);
    //在提交请求之前 测试连接是否可用
    params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true);
     
    PoolingClientConnectionManager conMgr = new PoolingClientConnectionManager();
    conMgr.setMaxTotal(200); //设置整个连接池最大连接数 根据自己的场景决定
    //是路由的默认最大连接(该值默认为2),限制数量实际使用DefaultMaxPerRoute并非MaxTotal。
    //设置过小无法支持大并发(ConnectionPoolTimeoutException: Timeout waiting for connection from pool),路由是对maxTotal的细分。
    conMgr.setDefaultMaxPerRoute(conMgr.getMaxTotal());//(目前只有一个路由,因此让他等于最大值)
     
    //另外设置http client的重试次数,默认是3次;当前是禁用掉(如果项目量不到,这个默认即可)
    httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
    复制代码

     此处解释下MaxtTotal和DefaultMaxPerRoute的区别:

    1、MaxtTotal是整个池子的大小;
    2、DefaultMaxPerRoute是根据连接到的主机对MaxTotal的一个细分;比如:
    MaxtTotal=400 DefaultMaxPerRoute=200
    而我只连接到http://xx.com时,到这个主机的并发最多只有200;而不是400;
    而我连接到 http://xx.com和 http://xxx.com时,到每个主机的并发最多只有200;即加起来是400(但不能超过400);所以起作用的设置是DefaultMaxPerRoute。
     
     
    2、httpclient 3.1
    复制代码
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setConnectionTimeout(2000);
    params.setSoTimeout(2000);
    // 最大连接数
    params.setMaxTotalConnections(500);
    params.setDefaultMaxConnectionsPerHost(500);
    params.setStaleCheckingEnabled(true);
    connectionManager.setParams(params);
     
    HttpClientParams httpClientParams = new HttpClientParams();
    // 设置httpClient的连接超时,对连接管理器设置的连接超时是无用的
    httpClientParams.setConnectionManagerTimeout(5000); //等价于4.2.3中的CONN_MANAGER_TIMEOUT
    httpClient = new HttpClient(connectionManager);
    httpClient.setParams(httpClientParams);
     
    //另外设置http client的重试次数,默认是3次;当前是禁用掉(如果项目量不到,这个默认即可)
    httpClientParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));
     
    复制代码

     参数类似 就不多解释了;

     

     

     

     

     

     另看一片转载内容

     

    HttpClient 4 和 HttpClient 3 设置超时

     

     


    HttpClient 4:
    连接超时:

    httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,60000);

     

    // 或者

    HttpConnectionParams.setConnectionTimeout(params, 6000);

     


    读取超时:

    httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,60000);

     

    // 或者

    HttpConnectionParams.setSoTimeout(params, 60000);

     


    HttpClient 3:
    连接超时:

    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(60000);

     

    读取超时:

    httpClient.getHttpConnectionManager().getParams().setSoTimeout(60000);

     

    知识只有共享才能传播,才能推崇出新的知识,才能学到更多,这里写的每一篇文字/博客,基本都是从网上查询了一下资料然后记录下来,也有些是原滋原味搬了过来,也有时加了一些自己的想法
  • 相关阅读:
    城市的划入划出效果
    文本溢出省略解决笔记css
    长串英文数字强制折行解决办法css
    Poj 2352 Star
    树状数组(Binary Indexed Trees,二分索引树)
    二叉树的层次遍历
    Uva 107 The Cat in the Hat
    Uva 10336 Rank the Languages
    Uva 536 Tree Recovery
    Uva10701 Pre, in and post
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13317687.html
Copyright © 2011-2022 走看看