Fidder能监听浏览器http请求是因为它启动一个代理服务器,浏览器通过这个代理服务器上网,但是eclipse里默认没有使用该代理
Stackoverflow提供了如下方法:
// set http proxy
System.setProperty("http.proxyHost", "localhost");
System.setProperty("http.proxyPort", "8888");
// set https proxy
System.setProperty("https.proxyHost", "localhost");
System.setProperty("https.proxyPort", "8888");
该方法在HttpURLConnection下的确可行。
但是HttpClients下就不可行了,设置对其无效
所以HttpClients需要如下设置代理:
CloseableHttpClient httpclient = HttpClients.createDefault();
//代理为弱网测试内容
HttpHost proxy = new HttpHost("localhost", 8888, "http");
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
HttpPost httpPost = new HttpPost(URL);
httpPost.setConfig(config);