Fiddler抓取IDEA上用HttpClient发出的网络请求
Posted 二木成林
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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设置了只抓取非浏览器请求
控制台也会成功输出
以上是关于Fiddler抓取IDEA上用HttpClient发出的网络请求的主要内容,如果未能解决你的问题,请参考以下文章
通过Fiddler抓取Java HttpClient的HTTP包
Fiddler抓取Intellij Idea中执行的web网络请求
完美解决用抓包工具抓取idea中使用HttpClient发送的HTTPHTTPS请求