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;
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);
Optional.ofNullable(param).ifPresent(h -> h.forEach(builder::addParameter));
HttpGet httpget = new HttpGet(builder.build());
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) {
HttpPost httpPost = new HttpPost(url);
Optional.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));
}
if (proxyVo.isSocks5()) {
return doExecuteSocks5(proxyVo, httpPost);
} else {
return doExecuteHttp(proxyVo, httpPost,url);
}
}
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 EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(response);
IOUtils.closeQuietly(httpclient);
}
return null;
}
public static String doExecuteHttp(ProxyVO proxyVo, HttpRequestBase httpRequest,String url) {
try {
HttpHost proxy = new HttpHost(proxyVo.getHost(), proxyVo.getPort(), Proxy.Type.HTTP.name());
URI uri = new URI(url);
HttpHost 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 {
@Override
public InetAddress[] resolve(String host) throws UnknownHostException {
return new InetAddress[]{InetAddress.getByAddress(new byte[]{1, 1, 1, 1})};
}
}
static class MyConnectionSocketFactory extends PlainConnectionSocketFactory {
@Override
public Socket createSocket(final HttpContext context) {
return new Socket(new Proxy(Proxy.Type.SOCKS, (InetSocketAddress) context.getAttribute("socks.address")));
}
@Override
public 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);
}
@Override
public Socket createSocket(final HttpContext context) {
return new Socket(new Proxy(Proxy.Type.SOCKS, (InetSocketAddress) context.getAttribute("socks.address")));
}
@Override
public 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();
}
}