HttpClient设置代理

在爬虫抓取受限网站数据时,可以利用HttpClient设置代理来规避IP限制。官方提供的代码示例提供了实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在做爬虫抓取网站数据时,通常可能会遇到一个问题,那就是服务器对访问IP做了一定的限制,比如限制单位时间内的访问次数等。这个时候,我们可以使用代理的方式进行解决。
我们可以使用HttpClient提供的方式进行代理设置,官方给出的代码示例如下:

package org.apache.http.examples.client;

import org.apache.http.HttpHost;
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;

/**
 * How to send a request via proxy.
 *
 * @since 4.0
 */
public class ClientExecuteProxy {

    public static void main(String[] args)throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpHost target = new HttpHost("localhost", 443, "https");  //这个是目标主机,即您要访问的服务器,比较网站。
            HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http");  //这个是代理信息,即我们要通过哪个代理主机进行代理请求访问。

            RequestConfig config = RequestConfig.custom()
                    .setProxy(proxy)
                    .build();
            HttpGet request = new HttpGet("/");
            request.setConfig(config);

            System.out.println("Executing request " + request.getRequestLine() + " to " + target + " via " + proxy);

            CloseableHttpResponse response = httpclient.execute(target, request);  //执行请求。
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());  //打印状态信息
                EntityUtils.consume(response.getEntity());
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值