Search code examples
javahttpproxyhttp-proxysocks

HTTP request through SOCKS protocol


I am trying to use Socks5 proxies to make http post requests. The provider for the proxies has disabled http or something. I get the following error;

Exception in thread "main" java.net.SocketException: SOCKS: Connection not allowed by ruleset

I am using the code

    System.setProperty("java.net.socks.username", user);
    System.setProperty("java.net.socks.password", pass);

    Proxy prox = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(ip, 8000));
    URL url = new URL("https://www.myip.com");
    URLConnection con = url.openConnection(prox);

    con.setConnectTimeout(10000);
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

    String line;
    while ((line = in.readLine()) != null) {
        System.out.println(line);
    }
    in.close();

How do I tunnel(?) this http request through the proxy?

PS I know this is possible because some extension in firefox allows me to use the proxy too I have also tried this code, but it results in the same error : https://pastebin.com/xt6evbm7


Solution

  • This system does not use the system properties.

            Authenticator.setDefault(new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(user, pass.toCharArray());
                }
            });