httpclient支持socks5和http代理调用接口

2024-09-04 06:04

本文主要是介绍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代理调用接口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1135201

相关文章

详解Java如何向http/https接口发出请求

《详解Java如何向http/https接口发出请求》这篇文章主要为大家详细介绍了Java如何实现向http/https接口发出请求,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 用Java发送web请求所用到的包都在java.net下,在具体使用时可以用如下代码,你可以把它封装成一

C#使用HttpClient进行Post请求出现超时问题的解决及优化

《C#使用HttpClient进行Post请求出现超时问题的解决及优化》最近我的控制台程序发现有时候总是出现请求超时等问题,通常好几分钟最多只有3-4个请求,在使用apipost发现并发10个5分钟也... 目录优化结论单例HttpClient连接池耗尽和并发并发异步最终优化后优化结论我直接上优化结论吧,

Java后端接口中提取请求头中的Cookie和Token的方法

《Java后端接口中提取请求头中的Cookie和Token的方法》在现代Web开发中,HTTP请求头(Header)是客户端与服务器之间传递信息的重要方式之一,本文将详细介绍如何在Java后端(以Sp... 目录引言1. 背景1.1 什么是 HTTP 请求头?1.2 为什么需要提取请求头?2. 使用 Spr

Idea调用WebService的关键步骤和注意事项

《Idea调用WebService的关键步骤和注意事项》:本文主要介绍如何在Idea中调用WebService,包括理解WebService的基本概念、获取WSDL文件、阅读和理解WSDL文件、选... 目录前言一、理解WebService的基本概念二、获取WSDL文件三、阅读和理解WSDL文件四、选择对接

Java调用Python代码的几种方法小结

《Java调用Python代码的几种方法小结》Python语言有丰富的系统管理、数据处理、统计类软件包,因此从java应用中调用Python代码的需求很常见、实用,本文介绍几种方法从java调用Pyt... 目录引言Java core使用ProcessBuilder使用Java脚本引擎总结引言python

Node.js 中 http 模块的深度剖析与实战应用小结

《Node.js中http模块的深度剖析与实战应用小结》本文详细介绍了Node.js中的http模块,从创建HTTP服务器、处理请求与响应,到获取请求参数,每个环节都通过代码示例进行解析,旨在帮... 目录Node.js 中 http 模块的深度剖析与实战应用一、引言二、创建 HTTP 服务器:基石搭建(一

Python如何实现 HTTP echo 服务器

《Python如何实现HTTPecho服务器》本文介绍了如何使用Python实现一个简单的HTTPecho服务器,该服务器支持GET和POST请求,并返回JSON格式的响应,GET请求返回请求路... 一个用来做测试的简单的 HTTP echo 服务器。from http.server import HT

java如何调用kettle设置变量和参数

《java如何调用kettle设置变量和参数》文章简要介绍了如何在Java中调用Kettle,并重点讨论了变量和参数的区别,以及在Java代码中如何正确设置和使用这些变量,避免覆盖Kettle中已设置... 目录Java调用kettle设置变量和参数java代码中变量会覆盖kettle里面设置的变量总结ja

高效+灵活,万博智云全球发布AWS无代理跨云容灾方案!

摘要 近日,万博智云推出了基于AWS的无代理跨云容灾解决方案,并与拉丁美洲,中东,亚洲的合作伙伴面向全球开展了联合发布。这一方案以AWS应用环境为基础,将HyperBDR平台的高效、灵活和成本效益优势与无代理功能相结合,为全球企业带来实现了更便捷、经济的数据保护。 一、全球联合发布 9月2日,万博智云CEO Michael Wong在线上平台发布AWS无代理跨云容灾解决方案的阐述视频,介绍了

如何在页面调用utility bar并传递参数至lwc组件

1.在app的utility item中添加lwc组件: 2.调用utility bar api的方式有两种: 方法一,通过lwc调用: import {LightningElement,api ,wire } from 'lwc';import { publish, MessageContext } from 'lightning/messageService';import Ca