3

Hi There Dear Community Members, i am a begginer and i am trying to setup a socks proxy with credentials so i used the following code. but it's not working.

proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("myhost.com", 81));
Authenticator.setDefault(new ProxyAuthenticator("aaa","aaa"));
conn = (HttpURLConnection) url.openConnection(proxy);

i am getting error

java.net.SocketException: SOCKS : authentication failed

i even tried the following code but its not working too.

proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("myhost.com", 81));
System.setProperty("java.net.socks.username","aaa");
System.setProperty("java.net.socks.password","aaa");

i tried to combine them, i tried to use System.setProperty("socksProxyUser","aaa") and System.setProperty("socksProxyPassword","aaa") but i am still getting the same error. Can Anyone Please Help me.

Thanks in Advance

1 Answer 1

3

Try this when attempting to authenticate.

public Authenticator getAuth(String user, String password) {
    new Authenticator() {
        public PasswordAuthentication getPasswordAuthentication() {
            return (new PasswordAuthentication(user, password.toCharArray()));
        }
    };
}

Then, normally initialize your proxy Like how youre doing, except with the authentication (The reason why youre getting an error), do this.

Authenticator.setDefault(getAuth(username, password));
1
  • 2
    is there a way to do this without Authenticator.setDefault()? my use case is multi threaded. Nov 7, 2022 at 21:32

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.