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.