通过Java语言配置IPNut 代理:
进入 IPNut 平台购买并获取信息,这里以静态IP为例(假如端口是 http://proxy.ipnut.com:28001,账号: ipnut,密码: 123456789)。
1. SOCKS5代理示例 #
import java.io.*;
import java.net.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class Socks5ProxyDemo {
// 代理配置
private static final String PROXY_HOST = "proxy.ipnut.com";
private static final int PROXY_PORT = 28001;
private static final String PROXY_USERNAME = "ipnut";
private static final String PROXY_PASSWORD = "123456789";
public static void main(String[] args) {
System.out.println("=== SOCKS5 代理演示 ===\n");
socks5WithHttpClient();
socks5WithSocket();
socks5WithCustomRequest();
}
/**
* 使用 Java 11+ HttpClient 的 SOCKS5 代理
*/
public static void socks5WithHttpClient() {
System.out.println("1. 使用 HttpClient 的 SOCKS5 代理:");
try {
// 创建 SOCKS5 代理
Proxy proxy = new Proxy(Proxy.Type.SOCKS,
new InetSocketAddress(PROXY_HOST, PROXY_PORT));
// 创建 HttpClient 使用 SOCKS5 代理
HttpClient client = HttpClient.newBuilder()
.proxy(ProxySelector.of(new InetSocketAddress(PROXY_HOST, PROXY_PORT)))
.connectTimeout(Duration.ofSeconds(30))
.build();
// 创建请求
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://httpbin.org/ip"))
.timeout(Duration.ofSeconds(30))
.GET()
.build();
// 发送请求
HttpResponse response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println("状态码: " + response.statusCode());
System.out.println("响应内容: " + response.body());
} catch (Exception e) {
System.err.println("请求失败: " + e.getMessage());
}
}
/**
* 使用 Socket 的 SOCKS5 代理(支持认证)
*/
public static void socks5WithSocket() {
System.out.println("\n2. 使用 Socket 的 SOCKS5 代理(带认证):");
Socket socket = null;
try {
// 创建代理
Proxy proxy = new Proxy(Proxy.Type.SOCKS,
new InetSocketAddress(PROXY_HOST, PROXY_PORT));
// 创建 Socket 连接
socket = new Socket(proxy);
socket.connect(new InetSocketAddress("httpbin.org", 80), 30000);
// 发送 HTTP 请求
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
// 构建 HTTP 请求
String request = "GET /ip HTTP/1.1\r\n" +
"Host: httpbin.org\r\n" +
"User-Agent: Java-SOCKS5-Proxy\r\n" +
"Connection: close\r\n\r\n";
out.println(request);
out.flush();
// 读取响应
String line;
StringBuilder response = new StringBuilder();
while ((line = in.readLine()) != null) {
response.append(line).append("\n");
}
System.out.println("响应内容:\n" + response.toString());
} catch (Exception e) {
System.err.println("Socket 请求失败: " + e.getMessage());
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* SOCKS5 代理自定义请求
*/
public static void socks5WithCustomRequest() {
System.out.println("\n3. SOCKS5 代理自定义请求:");
try {
// 设置 SOCKS5 代理系统属性
System.setProperty("socksProxyHost", PROXY_HOST);
System.setProperty("socksProxyPort", String.valueOf(PROXY_PORT));
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(30))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://httpbin.org/user-agent"))
.header("User-Agent", "Java-SOCKS5-Proxy-Demo/1.0")
.header("Accept", "application/json")
.timeout(Duration.ofSeconds(30))
.GET()
.build();
HttpResponse response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println("状态码: " + response.statusCode());
System.out.println("响应内容: " + response.body());
} catch (Exception e) {
System.err.println("自定义请求失败: " + e.getMessage());
} finally {
// 清理系统属性
System.clearProperty("socksProxyHost");
System.clearProperty("socksProxyPort");
}
}
}
2. HTTP代理示例 #
import java.io.*;
import java.net.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Base64;
public class HttpProxyDemo {
// 代理配置
private static final String PROXY_HOST = "proxy.ipnut.com";
private static final int PROXY_PORT = 28001;
private static final String PROXY_USERNAME = "ipnut";
private static final String PROXY_PASSWORD = "123456789";
public static void main(String[] args) {
System.out.println("=== HTTP 代理演示 ===\n");
httpProxyWithHttpClient();
httpProxyWithAuthenticator();
httpProxyWithSocket();
httpProxyMultipleRequests();
}
/**
* 使用 HttpClient 的 HTTP 代理
*/
public static void httpProxyWithHttpClient() {
System.out.println("1. 使用 HttpClient 的 HTTP 代理:");
try {
// 创建 HTTP 代理
Proxy proxy = new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(PROXY_HOST, PROXY_PORT));
// 创建 HttpClient 使用 HTTP 代理
HttpClient client = HttpClient.newBuilder()
.proxy(ProxySelector.of(new InetSocketAddress(PROXY_HOST, PROXY_PORT)))
.connectTimeout(Duration.ofSeconds(30))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://httpbin.org/ip"))
.timeout(Duration.ofSeconds(30))
.GET()
.build();
HttpResponse response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println("状态码: " + response.statusCode());
System.out.println("响应内容: " + response.body());
} catch (Exception e) {
System.err.println("请求失败: " + e.getMessage());
}
}
/**
* 使用 Authenticator 的 HTTP 代理认证
*/
public static void httpProxyWithAuthenticator() {
System.out.println("\n2. 使用 Authenticator 的 HTTP 代理认证:");
try {
// 设置代理认证
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
if (getRequestorType() == Authenticator.RequestorType.PROXY) {
return new PasswordAuthentication(PROXY_USERNAME,
PROXY_PASSWORD.toCharArray());
}
return null;
}
});
// 设置系统代理
System.setProperty("http.proxyHost", PROXY_HOST);
System.setProperty("http.proxyPort", String.valueOf(PROXY_PORT));
System.setProperty("https.proxyHost", PROXY_HOST);
System.setProperty("https.proxyPort", String.valueOf(PROXY_PORT));
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(30))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://httpbin.org/ip"))
.timeout(Duration.ofSeconds(30))
.GET()
.build();
HttpResponse response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println("状态码: " + response.statusCode());
System.out.println("响应内容: " + response.body());
} catch (Exception e) {
System.err.println("认证请求失败: " + e.getMessage());
} finally {
// 清理
Authenticator.setDefault(null);
System.clearProperty("http.proxyHost");
System.clearProperty("http.proxyPort");
System.clearProperty("https.proxyHost");
System.clearProperty("https.proxyPort");
}
}
/**
* 使用 Socket 的 HTTP 代理(手动认证)
*/
public static void httpProxyWithSocket() {
System.out.println("\n3. 使用 Socket 的 HTTP 代理(手动认证):");
Socket socket = null;
try {
// 连接到代理服务器
socket = new Socket(PROXY_HOST, PROXY_PORT);
// 构建代理认证信息
String auth = PROXY_USERNAME + ":" + PROXY_PASSWORD;
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes());
// 发送 CONNECT 请求到代理
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String connectRequest = "CONNECT httpbin.org:80 HTTP/1.1\r\n" +
"Host: httpbin.org:80\r\n" +
"Proxy-Authorization: Basic " + encodedAuth + "\r\n" +
"Connection: keep-alive\r\n\r\n";
out.print(connectRequest);
out.flush();
// 读取代理响应
String line;
while ((line = in.readLine()) != null) {
if (line.isEmpty()) break;
System.out.println("代理响应: " + line);
}
// 发送实际 HTTP 请求
String httpRequest = "GET /ip HTTP/1.1\r\n" +
"Host: httpbin.org\r\n" +
"User-Agent: Java-HTTP-Proxy-Demo\r\n" +
"Connection: close\r\n\r\n";
out.print(httpRequest);
out.flush();
// 读取 HTTP 响应
StringBuilder response = new StringBuilder();
while ((line = in.readLine()) != null) {
response.append(line).append("\n");
}
System.out.println("响应内容:\n" + response.toString());
} catch (Exception e) {
System.err.println("Socket 代理请求失败: " + e.getMessage());
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* HTTP 代理多请求演示
*/
public static void httpProxyMultipleRequests() {
System.out.println("\n4. HTTP 代理多请求演示:");
try {
// 设置代理
System.setProperty("http.proxyHost", PROXY_HOST);
System.setProperty("http.proxyPort", String.valueOf(PROXY_PORT));
// 设置认证
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(PROXY_USERNAME,
PROXY_PASSWORD.toCharArray());
}
});
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(30))
.build();
String[] testUrls = {
"http://httpbin.org/ip",
"http://httpbin.org/user-agent",
"http://httpbin.org/headers"
};
for (int i = 0; i < testUrls.length; i++) {
System.out.println("\n请求 " + (i + 1) + ": " + testUrls[i]);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(testUrls[i]))
.header("User-Agent", "Java-HTTP-Proxy-Multi/1.0")
.timeout(Duration.ofSeconds(30))
.GET()
.build();
HttpResponse response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println("状态码: " + response.statusCode());
System.out.println("响应内容: " + response.body().substring(0,
Math.min(200, response.body().length())) + "...");
Thread.sleep(1000); // 避免请求过快
}
} catch (Exception e) {
System.err.println("多请求失败: " + e.getMessage());
} finally {
// 清理
Authenticator.setDefault(null);
System.clearProperty("http.proxyHost");
System.clearProperty("http.proxyPort");
}
}
}
3. 代理测试工具 #
import java.net.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class ProxyTestTool {
private static final String PROXY_HOST = "proxy.ipnut.com";
private static final int PROXY_PORT = 28001;
private static final String PROXY_USERNAME = "ipnut";
private static final String PROXY_PASSWORD = "123456789";
public static void main(String[] args) {
System.out.println("=== 代理测试工具 ===\n");
testSocks5Proxy();
testHttpProxy();
}
/**
* 测试 SOCKS5 代理
*/
public static void testSocks5Proxy() {
System.out.println("测试 SOCKS5 代理:");
try {
// 设置 SOCKS5 代理
System.setProperty("socksProxyHost", PROXY_HOST);
System.setProperty("socksProxyPort", String.valueOf(PROXY_PORT));
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(15))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://httpbin.org/ip"))
.timeout(Duration.ofSeconds(15))
.GET()
.build();
HttpResponse response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println("✅ SOCKS5 代理连接成功");
System.out.println(" 状态码: " + response.statusCode());
System.out.println(" 响应: " + response.body());
} catch (Exception e) {
System.err.println("❌ SOCKS5 代理连接失败: " + e.getMessage());
} finally {
System.clearProperty("socksProxyHost");
System.clearProperty("socksProxyPort");
}
}
/**
* 测试 HTTP 代理
*/
public static void testHttpProxy() {
System.out.println("\n测试 HTTP 代理:");
try {
// 设置 HTTP 代理和认证
System.setProperty("http.proxyHost", PROXY_HOST);
System.setProperty("http.proxyPort", String.valueOf(PROXY_PORT));
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(PROXY_USERNAME,
PROXY_PASSWORD.toCharArray());
}
});
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(15))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://httpbin.org/ip"))
.timeout(Duration.ofSeconds(15))
.GET()
.build();
HttpResponse response = client.send(request,
HttpResponse.BodyHandlers.ofString());
System.out.println("✅ HTTP 代理连接成功");
System.out.println(" 状态码: " + response.statusCode());
System.out.println(" 响应: " + response.body());
} catch (Exception e) {
System.err.println("❌ HTTP 代理连接失败: " + e.getMessage());
} finally {
Authenticator.setDefault(null);
System.clearProperty("http.proxyHost");
System.clearProperty("http.proxyPort");
}
}
}
4. 编译和运行 #
# 编译所有Java文件
javac *.java
# 或者分别编译
javac Socks5ProxyDemo.java
javac HttpProxyDemo.java
javac ProxyTestTool.java
# 运行SOCKS5代理演示
java Socks5ProxyDemo
# 运行HTTP代理演示
java HttpProxyDemo
# 运行代理测试工具
java ProxyTestTool