本文主要是介绍httpclient支持socks5和http代理调用接口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
httpclient支持socks5和http代理调用接口
package com.ruoyi.web.controller.util;import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.DnsResolver;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.apache.poi.util.IOUtils;import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;/*** 支持socks5和http代理*/
public class HttpUtil {public static void main(String[] args) {String url = "http://ip-api.com/json/?lang=zh-CN";String http = "http://127.0.0.1:10163:admin:password";String socks = "socks5://127.0.0.1:20001:admin:password";ProxyVO socksVo = ProxyVO.resolver(socks);System.out.println(doGet(socksVo, url, null, null));ProxyVO httpVo = ProxyVO.resolver(http);System.out.println(doGet(httpVo, url, null, null));}public static String doGet(ProxyVO proxyVo, String url, Map<String, String> headers, Map<String, String> param) {try {URIBuilder builder = new URIBuilder(url);// 设置参数 builder.addParameter(key, param.get(key));Optional.ofNullable(param).ifPresent(h -> h.forEach(builder::addParameter));HttpGet httpget = new HttpGet(builder.build());// 设置header setHeader(key, headers.get(key));Optional.ofNullable(headers).ifPresent(h -> h.forEach(httpget::setHeader));if (proxyVo.isSocks5()) {return doExecuteSocks5(proxyVo, httpget);} else {return doExecuteHttp(proxyVo, httpget,url);}} catch (Exception e) {e.printStackTrace();}return null;}public static String doPost(ProxyVO proxyVo, String url, Map<String, String> headers, Map<String, String> param) {// 创建Http Post请求HttpPost httpPost = new HttpPost(url);// 设置headerOptional.ofNullable(headers).ifPresent(h -> h.forEach(httpPost::setHeader));// 创建参数列表if (param != null) {List<NameValuePair> paramList = param.entrySet().stream().map(entry -> new BasicNameValuePair(entry.getKey(), entry.getValue())).collect(Collectors.toList());// 模拟表单httpPost.setEntity(new UrlEncodedFormEntity(paramList, StandardCharsets.UTF_8));}// 执行http请求if (proxyVo.isSocks5()) {return doExecuteSocks5(proxyVo, httpPost);} else {return doExecuteHttp(proxyVo, httpPost,url);}}/*** Socks5 代理*/public static String doExecuteSocks5(ProxyVO proxyVo, HttpUriRequest httpRequest) {CloseableHttpClient httpclient = null;CloseableHttpResponse response = null;try {//用户名和密码验证Authenticator.setDefault(new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(proxyVo.getUserName(), proxyVo.getPwd().toCharArray());}});httpclient = HttpClients.custom().setConnectionManager(new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create().register("http", new MyConnectionSocketFactory()).register("https", new MySSLConnectionSocketFactory(SSLContexts.createSystemDefault())).build(),new FakeDnsResolver())).build();HttpClientContext context = HttpClientContext.create();context.setAttribute("socks.address", new InetSocketAddress(proxyVo.getHost(), proxyVo.getPort()));response = httpclient.execute(httpRequest, context);if (response.getStatusLine().getStatusCode() == 200) {
// return new String(EntityUtils.toByteArray(response.getEntity()), StandardCharsets.UTF_8);return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);}} catch (Exception e) {e.printStackTrace();} finally {IOUtils.closeQuietly(response);IOUtils.closeQuietly(httpclient);}return null;}/*** http 代理*/public static String doExecuteHttp(ProxyVO proxyVo, HttpRequestBase httpRequest,String url) {try {// 设置代理HttpHostHttpHost proxy = new HttpHost(proxyVo.getHost(), proxyVo.getPort(), Proxy.Type.HTTP.name());URI uri = new URI(url);// 设置要访问的HttpHost,即是目标站点的HttpHostHttpHost httpTarget = new HttpHost(uri.getHost(), uri.getPort());// 设置认证CredentialsProvider provider = new BasicCredentialsProvider();provider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials(proxyVo.getUserName(), proxyVo.getPwd()));CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();RequestConfig config = RequestConfig.custom().setProxy(proxy).build();httpRequest.setConfig(config);CloseableHttpResponse response = httpClient.execute(httpTarget, httpRequest);if (response.getStatusLine().getStatusCode() == 200) {return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);}} catch (IOException | URISyntaxException e) {e.printStackTrace();}return null;}static class FakeDnsResolver implements DnsResolver {@Overridepublic InetAddress[] resolve(String host) throws UnknownHostException {return new InetAddress[]{InetAddress.getByAddress(new byte[]{1, 1, 1, 1})};}}static class MyConnectionSocketFactory extends PlainConnectionSocketFactory {@Overridepublic Socket createSocket(final HttpContext context) {return new Socket(new Proxy(Proxy.Type.SOCKS, (InetSocketAddress) context.getAttribute("socks.address")));}@Overridepublic Socket connectSocket(int connectTimeout, Socket socket, HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpContext context) throws IOException {return super.connectSocket(connectTimeout, socket, host, InetSocketAddress.createUnresolved(host.getHostName(), remoteAddress.getPort()), localAddress, context);}}static class MySSLConnectionSocketFactory extends SSLConnectionSocketFactory {public MySSLConnectionSocketFactory(final SSLContext sslContext) {super(sslContext, ALLOW_ALL_HOSTNAME_VERIFIER);}@Overridepublic Socket createSocket(final HttpContext context) {return new Socket(new Proxy(Proxy.Type.SOCKS, (InetSocketAddress) context.getAttribute("socks.address")));}@Overridepublic Socket connectSocket(int connectTimeout, Socket socket, HttpHost host, InetSocketAddress remoteAddress,InetSocketAddress localAddress, HttpContext context) throws IOException {return super.connectSocket(connectTimeout, socket, host, InetSocketAddress.createUnresolved(host.getHostName(),remoteAddress.getPort()), localAddress, context);}}
}
package com.ruoyi.web.controller.util;import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.stream.Stream;@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ProxyVO {// 代理信息private boolean isSocks5;private String host;private int port;private String userName;private String pwd;public static ProxyVO resolver(String proxyUrl) {if (StrUtil.isEmpty(proxyUrl)) return null;String prefix = Stream.of("socks5://", "http://").filter(proxyUrl::startsWith).findFirst().orElse(null);if (prefix == null) return null;String[] split = StrUtil.replace(proxyUrl, prefix, "").split(":");if (split.length != 4) return null;return ProxyVO.builder().isSocks5(StrUtil.startWith(proxyUrl, "socks5://")).host(split[0]).port(Integer.parseInt(split[1])).userName(split[2]).pwd(split[3]).build();}
}
这篇关于httpclient支持socks5和http代理调用接口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!