2

I'm trying to use password authentication if a proxy requires it for Java's Native HTTP Client requests.

I have tried a bunch of stuff, with the most common solution applied to my program being

String username = "username";
String password = "password";

Authenticator.setDefault( new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password.toCharArray());
            }
        });

But after that I'm still getting error 407 when sending the request (which works fine on an IP Authenticated proxy). The proxy works just fine on a browser running it through a Chrome extension.

metters
  • 533
  • 1
  • 8
  • 17

2 Answers2

0

If you are trying to authenticate to proxy you normally need to add Proxy-Authorization header in your request.

See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authorization

Something like

String authentication = "username:password";
String authenticationEncoded = Base64.getEncoder().encodeToString(authentication .getBytes());
HttpHeaders headers = new HttpHeaders();
headers.add("Proxy-Authorization", "Basic " + authenticationEncoded );
ikettu
  • 1,203
  • 12
  • 17
0

Like the HttpURLConnection the java.net.http.HttpClient honors some system properties that allow to control (enable/disable) some proxy authentication schemes. If your proxy requires authentication you may have to tune the default values of these properties (jdk.http.auth.tunneling.disabledSchemes and jdk.http.auth.proxying.disabledSchemes).
See:
core-libs/java.net
Disable Basic authentication for HTTPS tunneling
From https://www.oracle.com/technetwork/java/javase/8all-relnotes-2226344.html

daniel
  • 2,665
  • 1
  • 8
  • 18