0

I was trying to connect to internet with a Squid HTTP Proxy with Java, I looked at

Authenticated HTTP proxy with Java

but that returns Failed to authenticate with proxy. Here is my code so far:

String proxyHost = config.getString("ProxyHost").trim();
        String proxyPort = config.getString("ProxyPort").trim();
        final String authUser = config.getString("ProxyUser").trim();
        final String authPassword = config.getString("ProxyPw").trim();

        if (!proxyHost.isEmpty() && proxyHost != null) {
            DiscordSRV.debug("Proxy host setting to " + proxyHost);

            Authenticator.setDefault(
                    new Authenticator() {
                        @Override
                        public PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(authUser, authPassword.toCharArray());
                        }
                    });

            try {

                System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
                // HTTP proxy
                System.setProperty("http.proxyHost", proxyHost);
                System.setProperty("http.proxyPort", proxyPort);
                System.setProperty("http.proxyUser", authUser);
                System.setProperty("http.proxyPassword", authUser);

                // HTTPS proxy
                System.setProperty("https.proxyHost", proxyHost);
                System.setProperty("https.proxyPort", proxyPort);
                System.setProperty("https.proxyUser", authUser);
                System.setProperty("https.proxyPassword", authUser);

                DiscordSRV.debug("Proxy successfully set");
            } catch (Exception e) {
                DiscordSRV.debug("Proxy settings failed to apply");
            }
        }

I have already tested the proxy with cURL and the proxy works fine.

YueYue
  • 13
  • 4
  • You're setting your password to the user name. – tgdavies Jul 01 '23 at 09:11
  • I corrected that by `System.setProperty("https.proxyPassword", authPassword);` but that still gives `java.io.IOException: Failed to authenticate with proxy` – YueYue Jul 01 '23 at 09:24
  • Did you fix "http.proxyPassword" as well? – tgdavies Jul 01 '23 at 09:25
  • Yes I did, `System.setProperty("http.proxyPassword", authPassword);` – YueYue Jul 01 '23 at 09:26
  • I add some `System.out.println()` and is seem that it print the username and password correctly – YueYue Jul 01 '23 at 09:26
  • I found from squid connection log that all the connections that fails have no username but the connection tested with cURL is with username, does that mean I didnt set username correctly? – YueYue Jul 01 '23 at 09:32

0 Answers0