如何在 java 小程序中连接到 SOCKS 代理

How to connect to a SOCKS proxy in a java applet

我制作了一个 Java 访问网页的小程序。我需要它通过 SOCKS 代理连接到网站我已经尝试将这段代码放在我的程序初始化的地方:

System.setProperty("socksProxyHost", "66.85.144.228");
System.setProperty("socksProxyPort", "1080");

但似乎什么都没发生,它只是使用我的正常 IP 地址?

您的属性可能是 being set too-late, after they have already been read by the relevant code when it initializes, but you are also probably hitting the security restrictions of a sandboxed applet。您的小程序是否已签名,还是 运行 沙盒化?

如果这是一个 Java 应用程序而不是小程序,您可以通过在 JVM 启动时设置这些来测试它,例如"-DsocksProxyHost=66.85.144.228 -DsocksProxyPort=1080".

由于您使用的是小程序,因此您可以设置的系统属性受到限制。你可以 set deployment parameters:

<APPLET archive="my_applet.jar" code="MyApplet" width="300" height="300">
    <PARAM name="java_arguments" value="-DsocksProxyHost=66.85.144.228">
</APPLET>

...但是 socksProxyHost 当然不在 list of trusted/secure properties 中,因此您的小程序需要完全签名才能运行。

您可以通过 java.net.Proxy.

以编程方式完成