目 录CONTENT

文章目录

Java HttpClient代理设定

BKUN
2021-11-13 / 0 评论 / 0 点赞 / 2,018 阅读 / 754 字
public static void main(String[] args) throws Exception {
        HttpPost httpPost = new HttpPost("xxx");
        // 代理配置开始 (例 fiddler 代理端口号 8888)
        HttpHost proxy = new HttpHost("127.0.0.1",8888);
        RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).build();
        CloseableHttpClient httpclient= HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
        // 代理配置结束
        
        HttpResponse httpResponse = httpclient.execute(httpPost);
        int statusCode = httpResponse.getStatusLine().getStatusCode();

        System.out.println(httpResponse.getStatusLine());

        String result = null;

        if (statusCode == HttpStatus.SC_OK) {
            HttpEntity resEntity = httpResponse.getEntity();
            result = EntityUtils.toString(resEntity);
        }

        httpclient.getConnectionManager().shutdown();
        
        System.out.println("result:"+result);
}
0

评论区