Java 代理接入文档
学习如何使用Java的HttpURLConnection接入718Proxy代理服务
代理配置信息
// 动态住宅代理 / 5G移动代理通用配置
String proxyHost = "resi.718proxy.com";
int proxyPort = 5001;
String proxyUsername = "您的账号";
String proxyPassword = "您的密码";代码示例
Java
// 使用 Java 接入718Proxy代理
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.util.Base64;
public class ProxyExample {
public static void main(String[] args) {
// 代理配置
String proxyHost = "resi.718proxy.com";
int proxyPort = 5001;
String proxyUsername = "您的账号";
String proxyPassword = "您的密码";
try {
// 创建代理对象
Proxy proxy = new Proxy(
Proxy.Type.HTTP,
new InetSocketAddress(proxyHost, proxyPort)
);
// 测试URL
URL url = new URL("https://httpbin.org/ip");
HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
// 设置代理认证
String auth = proxyUsername + ":" + proxyPassword;
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes());
connection.setRequestProperty("Proxy-Authorization", "Basic " + encodedAuth);
// 设置请求属性
connection.setRequestMethod("GET");
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
// 读取响应
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream())
);
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
System.out.println("状态码: " + connection.getResponseCode());
System.out.println("响应内容: " + response.toString());
} catch (Exception e) {
System.err.println("请求失败: " + e.getMessage());
}
}
}