我正在尝试在我的 Java 应用程序中使用 TOR 作为代理。这是我用于测试的基本代码:
SocketAddress sockAddr = new InetSocketAddress("localhost",9050);
Proxy proxy = new Proxy(Proxy.Type.SOCKS,sockAddr);
URL url = new URL("https://check.torproject.org/");
//URL url = new URL("https://api.ipify.org/");
url.openConnection(proxy);
InputStream in = url.openStream();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] stuff = new byte[1024];
int readBytes = 0;
while((readBytes = in.read(stuff))>0) {
bout.write(stuff,0,readBytes);
}
byte[] result = bout.toByteArray();
System.out.print(new String(result));
Run Code Online (Sandbox Code Playgroud)
结果字符串是 -
<!doctype html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>
Sorry. You are not using Tor.
</title>
<link rel="icon" type="image/x-icon" …Run Code Online (Sandbox Code Playgroud)