HTTP请求使用http、socks代理demo,包含有认证和无认证
package cn.daenx.myadmin.email.utils;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import java.net.*;
/**
* HTTP请求使用http、socks代理demo,包含有认证和无认证
*
* @author DaenMax
*/
public class HttpProxyReqDemo {
public static void main(String[] args) {
//Hutool普通请求
HutoolReq();
//Hutool使用无认证的代理
HutoolReqProxy();
//Hutool使用有认证的代理
HutoolReqProxyAuth();
//Java.net使用有认证的代理
JavaNetReqProxyAuth();
}
/**
* Hutool普通请求
*/
public static void HutoolReq() {
HttpRequest request = HttpUtil.createGet("https://www.baidu.com/");
String body = request.execute().body();
System.out.println(body);
}
/**
* Hutool使用无认证的代理
*/
public static void HutoolReqProxy() {
HttpRequest request = HttpUtil.createGet("https://www.baidu.com/");
request = request.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890)));
// request = request.setProxy(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("mylisg.fun", 36160)));
String body = request.execute().body();
System.out.println(body);
}
/**
* Hutool使用有认证的代理
*/
public static void HutoolReqProxyAuth() {
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("CRsKrsNzJ0", "5tBgyWKrgy".toCharArray());
}
});
HttpRequest request = HttpUtil.createGet("https://www.baidu.com/");
request = request.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890)));
// request = request.setProxy(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("mylisg.fun", 36160)));
String body = request.execute().body();
System.out.println(body);
}
/**
* Java.net使用有认证的代理
*
* @throws Exception
*/
public static void JavaNetReqProxyAuth() {
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("CRsKrsNzJ0", "5tBgyWKrgy".toCharArray());
}
});
try {
// 设置代理服务器地址和端口
// Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("mylisg.fun", 36160));
// 创建URL对象
URL url = new URL("https://www.baidu.com/");
// 打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
System.out.println("Response Code: " + responseCode);
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
本文作者:DaenMax
本文链接:https://www.cnblogs.com/daen/p/18193202
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
分类:
JAVA:日常
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2023-05-15 Gitee、Github上star星星数获取到一个图片里,用于MD文档