简体   繁体   中英

How to run java behind proxychains

I have a java class in which htmlunit's webclient gets html pages. I want to send the packets through Tor proxy. When I set the proxy in my java code, by setting System properties:

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

, it works correctly. But when I want to use tool ProxyChains,

proxychains java -jar MyPackagedJava.jar

, it is not work! In the other words, I want to send htmlunit's packets through ProxyChains. How to do it?

HtmlUnit does not use the java proxy settings, you have to configure this during your client setup. Have a look at HtmlUnit -Getting started ; there is a sample for proxy setup.

Java doesn't send the packets via proxychains' proxies. So, you have to set the proxy in your coding. For example when using HtmlUnit & WebClient object to request a web page, just use the followings:

**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);
    }

The object proxy is an instance of Proxy class defined below:

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;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM