如何在代理链后面运行java

时间:2017-10-02 08:27:10

标签: java proxy tor packets

我有一个java类,其中htmlunit的webclient获取html页面。我想通过Tor代理发送数据包。当我在我的java代码中设置代理时,通过设置系统属性:

System.getproperty("socksProxyHost","127.0.0.1");
System.getproperty("socksProxyPort","9050");

,它正常工作。但是当我想使用工具ProxyChains时,

proxychains java -jar MyPackagedJava.jar

,这不行! 换句话说,我想通过ProxyChains发送htmlunit的数据包。怎么做?

2 个答案:

答案 0 :(得分:1)

HtmlUnit不使用java代理设置,您必须在客户端设置期间配置它。 看看HtmlUnit -Getting started;有一个代理设置示例。

答案 1 :(得分:0)

Java不通过代理链代理发送数据包。因此,您必须在编码中设置代理。例如,当使用HtmlUnit& WebClient对象请求一个网页,只需使用以下内容:

**WebClient webclient;**
if (proxy != null && !proxy.getHost().trim().equalsIgnoreCase("")) {
        if (proxy.getType() == null || proxy.getType().trim().equalsIgnoreCase("")
                || proxy.getType().trim().equalsIgnoreCase("http") ||
                proxy.getType().trim().equalsIgnoreCase("https"))
            webclient = new WebClient(BrowserVersion.CHROME, proxy.getHost(), proxy.getPort());
        else if (proxy.getType().trim().equalsIgnoreCase("socks")) {
            System.setProperty("socksProxyHost", proxy.getHost());
            System.setProperty("socksProxyPort", String.valueOf(proxy.getPort()));
            webclient = new WebClient(BrowserVersion.CHROME);
        }
        webclient.getCredentialsProvider().setCredentials(AuthScope.ANY, new NTCredentials(
                proxy.getUsername(), proxy.getPassword(), "", ""));

    } else {
        webclient = new WebClient(BrowserVersion.CHROME);
    }

对象代理是下面定义的代理类的实例:

public class Proxy {
    private String host, username, password,type;
    private int port;


public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public Proxy(String host, int port, String username, String password) {
    this.host = host;
    this.port = port;
    this.username = username;
    this.password = password;
}

public void setHost(String host) {
    this.host = host;
}

public String getHost() {
    return this.host;
}

public void setUsername(String username) {
    this.username = username;
}

public String getUsername() {
    return this.username;
}

public void setPassword(String password) {
    this.password = password;
}

public String getPassword() {
    return this.password;
}

public void setPort(int port) {
    this.port = port;
}

public int getPort() {
    return this.port;
}

}