生活如麻 发表于 2017-12-24 07:20:38

使用Apache HttpClient 4.5设置超时时间

import java.io.IOException;  

  
import org.apache.http.Consts;
  
import org.apache.http.client.ClientProtocolException;
  
import org.apache.http.client.config.RequestConfig;
  
import org.apache.http.client.methods.CloseableHttpResponse;
  
import org.apache.http.client.methods.HttpGet;
  
import org.apache.http.impl.client.CloseableHttpClient;
  
import org.apache.http.impl.client.HttpClients;
  
import org.apache.http.util.EntityUtils;
  

  
/**
  
* @author
  
*
  
* @date 2017年5月18日 上午9:17:08
  
*
  
* @Description
  
*/

  
public>  

  
   /**
  
      * @param args
  
      * @throws IOException
  
      * @throws ClientProtocolException
  
      */
  
   public static void main(String[] args) throws ClientProtocolException, IOException {
  
         CloseableHttpClient httpclient = HttpClients.createDefault();
  
         HttpGet httpGet = new HttpGet("http://stackoverflow.com/");
  
         RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(1000)
  
               .setSocketTimeout(5000).build();
  
         httpGet.setConfig(requestConfig);
  
         CloseableHttpResponse response = httpclient.execute(httpGet);
  
         System.out.println(response.getStatusLine());// 得到状态行
  
         System.out.println(EntityUtils.toString(response.getEntity(), Consts.UTF_8.name()));// 得到请求回来的数据
  
   }
  
}
页: [1]
查看完整版本: 使用Apache HttpClient 4.5设置超时时间