Java爬虫入门(1)

HttpClient-Get(不带参数)        

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://www.itcast.cn/");
CloseableHttpResponse response = null;
try {
    response = httpClient.execute(httpGet);
    if (response.getStatusLine().getStatusCode()==200){
        String content = EntityUtils.toString(response.getEntity(), "GBK");
        System.out.println(content.length());
    }
} catch (IOException e) {
    e.printStackTrace();
}finally {
    try {
        response.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        httpClient.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

HttpClient-Get(带参数)  

CloseableHttpClient httpClient = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder("http://www.itcast.cn/search");
uriBuilder.addParameter("keys","java");
HttpGet httpGet = new HttpGet( uriBuilder.build());
CloseableHttpResponse response = null;
try {
    response = httpClient.execute(httpGet);
    if (response.getStatusLine().getStatusCode()==200){
        String content = EntityUtils.toString(response.getEntity(), "GBK");
        System.out.println(content.length());
    }
} catch (IOException e) {
    e.printStackTrace();
}finally {
    try {
        response.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        httpClient.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

   HttpClient-Post

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://www.itcast.cn/");
CloseableHttpResponse response = null;
try {
    response = httpClient.execute(httpPost);
    if (response.getStatusLine().getStatusCode()==200){
        String content = EntityUtils.toString(response.getEntity(), "GBK");
        System.out.println(content.length());
    }
} catch (IOException e) {
    e.printStackTrace();
}finally {
    try {
        response.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        httpClient.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

   HttpClient-Post(带参数)

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://www.itcast.cn/");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("keys","java"));
UrlEncodedFormEntity forEntity = new UrlEncodedFormEntity(params, "utf8");
httpPost.setEntity(forEntity);
CloseableHttpResponse response = null;
try {
    response = httpClient.execute(httpPost);
    if (response.getStatusLine().getStatusCode()==200){
        String content = EntityUtils.toString(response.getEntity(), "GBK");
        System.out.println(content.length());
    }
} catch (IOException e) {
    e.printStackTrace();
}finally {
    try {
        response.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        httpClient.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

  连接池:        

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();cm.setMaxTotal(100);//最大连接数
cm.setDefaultMaxPerRoute(10);//设置每个主机的最大连接数
CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build();

配置请求设置信息:

        

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(100);//最大连接数
cm.setDefaultMaxPerRoute(10);//设置每个主机的最大连接数
CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build();
HttpGet httpGet = new HttpGet("http://www.itcast.cn/");
RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(1000)
        .setConnectTimeout(500)//设置获取连接的最长时间,单位是毫秒
        .setSocketTimeout(10 * 1000)//设置数据传输的最长时间,单位是毫秒
        .build();
httpGet.setConfig(config);

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值