Fiddler抓取IDEA上用HttpClient发出的网络请求

原先使用HttpClient发送请求的代码是这样的:

    public static void testHttpClient() throws IOException {
        CloseableHttpClient client = HttpClientBuilder.create().build();
        String url = "http://www.baidu.com/";
        HttpGet get = new HttpGet(url);
        try {
            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity, "UTF-8");
            System.out.println("url: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

但是我们并不能看到HTTP的请求和响应,所以需要使用Fiddler来抓包。

打开Fiddler

 查看端口号,该端口号可以修改。

 接着在IDEA中修改代码为:

    public static void testHttpClientByProxy() throws IOException {
        // 设置代理,hostname参数是本机的IP地址,port端口号是Fiddler设置的端口号
        HttpHost proxy = new HttpHost("192.168.33.35", 8888, "http");
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        // 设置配置
        CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
        String url = "http://www.baidu.com/";
        HttpGet get = new HttpGet(url);
        try {
            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity, "UTF-8");
            System.out.println("url: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

核心是如下配置:

注意IP地址和端口号。

但最值得注意的还是导包,有同名的类, 应该导入org.apache.http.HttpHost,而不是httpclient包中的,尽管我们使用的是HttpClient,这个必须注意。

所以完整的代码如下:

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class Demo {
    public static void main(String[] args) {
        testHttpClientByProxy();
//        testHttpClient();
    }

    public static void testHttpClientByProxy() {
        // 设置代理,hostname参数是本机的IP地址,port端口号是Fiddler设置的端口号
        HttpHost proxy = new HttpHost("192.168.33.55", 8888, "http");
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        // 设置配置
        CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
        String url = "http://www.baidu.com/";
        HttpGet get = new HttpGet(url);
        try {
            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity, "UTF-8");
            System.out.println("url: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void testHttpClient() {
        CloseableHttpClient client = HttpClientBuilder.create().build();
        String url = "http://www.baidu.com/";
        HttpGet get = new HttpGet(url);
        try {
            HttpResponse response = client.execute(get);
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity, "UTF-8");
            System.out.println("url: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

我们是使用百度作为测试网站的,启动IDEA中的程序,查看Fiddler

 注意,在Fiddler设置了只抓取非浏览器请求

 控制台也会成功输出

延申:如果是使用hutool的类进行HTTP请求,设置代理的方法如下:

String responseContent=HttpRequest.get(url).setHttpProxy("192.168.33.55",8888).execute().body();

设置后,也可以通过Fiddler抓到发送出去的请求。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值