zoukankan      html  css  js  c++  java
  • httpClient创建对象、设置超时

    从老版本和新版本进行比较说明:

    1.创建HttpClient对象

    3.X:

    HttpClient httpClient = new DefaultHttpClient();

    4.3:

    CloseableHttpClient httpClient = HttpClients.createDefault();

    2.超时设置:

    3.X:

    HttpClient httpClient = new HttpClient();
    client.setConnectionTimeout(30000); 
    client.setTimeout(30000);

    或者:

    HttpClient httpClient= new HttpClient(); 
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

    4.X(4.3后已过时):

    HttpClient httpClient=new DefaultHttpClient();
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,2000);//连接时间
    httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,2000);    //数据传输时间

    4.3:

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost=new HttpPost("http://www.baidu.com");    //HTTP post请求
    // 请求获取数据的超时时间 、 设置从connect Manager获取Connection超时时间、设置连接超时时间
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectionRequestTimeout(3000).setConnectTimeout(10000).build();
    httpPost.setConfig(requestConfig);

    3.httpclient 4.3.1,当不设置 超时时间的时候

      如果请求的url是通的,但服务器没有响应,会一直等待响应;
      如果请求的url是不通的,21秒后会报:Connection timed out: connect

  • 相关阅读:
    chrome shortkeys
    五笔xu
    getline()报错解决办法
    PCA程序
    c++矩阵运算库Eigen
    yolo接口
    遇到的编译错误及解决办法
    visual studio command window的使用
    NDK+MSYS2+Android sdk编译opencv源码
    面向对象分析与设计笔记(三)
  • 原文地址:https://www.cnblogs.com/xbq8080/p/7372547.html
Copyright © 2011-2022 走看看