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);
        // 设置header
        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));
        }
        // 执行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 {
            // 设置代理HttpHost
            HttpHost proxy = new HttpHost(proxyVo.getHost(), proxyVo.getPort(), Proxy.Type.HTTP.name());

            URI uri = new URI(url);
            // 设置要访问的HttpHost,即是目标站点的HttpHost
            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();
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值