Java小程序和代理

时间:2014-12-28 18:36:50

标签: java networking proxy applet socks

假设我使用以下代码连接到SOCKS5代理,我实例化的applet发送的连接或数据包是否会通过同一个代理?

System.getProperties().setProperty("socksProxySet", "true");
System.getProperties().setProperty("socksProxyHost", "*.*.*.*");
System.getProperties().setProperty("socksProxyPort", "*");

Applet是使用从中创建newInstance的类加载器对象启动的。

classLoader = new CustomClassLoader(/* Hashmap of byte arrays */); // Custom classloader that works using byte arrays
Applet applet = (Applet) classLoader.loadClass("class").newInstance();
applet.setStub(stub);
applet.init();
applet.start();
frame.add(applet);

2 个答案:

答案 0 :(得分:2)

这个问题的答案似乎是

以下代码:

public class TestApplet extends Applet {

    private String ip;

    public void init() {
        try {
            URL ipCheck = new URL("http://checkip.amazonaws.com");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(ipCheck.openStream()));
            ip = bufferedReader.readLine();
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void stop() {
    }

    public void paint(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.CYAN);
        g.drawString("Current IP: " + ip, 10, 20);
    }

}

public class Boot {

    public static void main(String[] args) {
        System.getProperties().setProperty("socksProxySet", "true");
        System.getProperties().setProperty("socksProxyHost", "71.9.127.141"); //Credits to HideMyAss.com
        System.getProperties().setProperty("socksProxyPort", "28045");//Credits to HideMyAss.com

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                TestApplet testApplet = new TestApplet();
                testApplet.init();
                JFrame jFrame = new JFrame();
                jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                jFrame.setSize(500, 500);
                jFrame.setContentPane(testApplet);
                jFrame.setVisible(true);
            }
        }).start();
    }

}    

输出:

enter image description here

答案 1 :(得分:1)

泰勒,如果我理解你的问题 - 是的。但这有一个警告:您设置的系统属性将仅在当前JVM实例化中保留。

因此,如果您的示例显示您设置属性并在同一JVM上执行Applet,那么您应该没问题。