Java爬虫HttpClient -- 配置请求参数

package xxx.xxx.xxx;
import org.apache.http.HttpEntity;
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;
import java.io.IOException;


public class HttpConfigTest {
     

    public static void main(String[] args)  {
     
        //1. 打开浏览器,创建HTTPClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //2.输入网址,发起get请求创建HttpGet对象
        HttpGet httpGet = new HttpGet("http://www.itcast.cn");

        //配置请求信息
        RequestConfig  config = RequestConfig.custom().setConnectTimeout(1000)  //创建连接的最长时间,单位是毫秒
                .setConnectionRequestTimeout(500) //设置获取连接的最长时间,单位是毫秒
                .setSocketTimeout(10*1000)  //设置数据传输的最长时间,单位是毫秒
                .build();
        //  给请求设置请求信息
        httpGet.setConfig(config);

        CloseableHttpResponse response = null;
       try{
     
           response = httpClient.execute(httpGet);

           //4.解析响应,获取数据
           if( response.getStatusLine().getStatusCode() == 200){
     
               HttpEntity httpEntity = response.getEntity();
               String content = EntityUtils.toString(httpEntity, "utf8");
               System.out.println(content);
           }
       }catch(IOException e){
     
           e.printStackTrace();
       }finally {
     
           //关闭response
           try{
     
               response.close();
           }catch(IOException e){
     
               e.printStackTrace();
           }
            try{
     
                httpClient.close();
            }catch(IOException e){
     
                e.printStackTrace();
            }
       }


    }
}

你可能感兴趣的:(Java,java,httpClient)