Java代码里面设置代理

   CloseableHttpClient httpClient = HttpClients.createDefault();


//把代理设置到请求配置 代理IP 端口 192.2120.222.226

HttpHost proxy = new HttpHost("192.2120.222.226", 80);
//超时时间单位为毫秒
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setConnectTimeout(2000).setSocketTimeout(2000)
.setProxy(proxy).build();
httpClient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();

HttpResponse res = httpClient.execute(post);

//设置URLConnection 的代理

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));  
URLConnection conn = url.openConnection(proxy); 
posted @ 2021-06-25 11:31  易不易  阅读(568)  评论(0编辑  收藏  举报