ARTICLE AD BOX
I had code that was using the old HttpURLConnection class to make calls to a web API. I was trying to convert that code to use HttpClient instead so that I could make a PATCH call, as the old Java Http classes do not support PATCH.
I am also behind a proxy, and with my old code, I never had to authenticate to the proxy or do anything special for it because I set the property java.net.useSystemProxies to true. However, that seems to have no effect when using HttpClient. Thus, I attempted to follow multiple tutorials on setting up proxy authentication with HttpClient, but no matter what I do, I cannot get it to actually use the Authenticator (the getPasswordAuthentication() method is never called), and it still returns a 407 status code.
This is the simple code that I am running that will not make it past the proxy server:
public static void main(String[] args) { String url = "http://www.google.com/"; String proxyHost = "10.10.10.1"; int proxyPort = 8080; //Obviously these fields have real values when run String authUser = "user"; String authPassword = "password"; ProxySelector proxySelector = ProxySelector.of(new InetSocketAddress(proxyHost, proxyPort)); Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(authUser, authPassword.toCharArray()); } }; HttpRequest.Builder builder = HttpRequest.newBuilder(); try { builder.uri(new URI(url)); } catch (URISyntaxException e) { e.printStackTrace(); } builder.method("GET", HttpRequest.BodyPublishers.noBody()); HttpRequest request = builder.build(); try (HttpClient client = HttpClient.newBuilder().proxy(proxySelector).authenticator(authenticator).build()) { HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.statusCode()); System.out.println(response.body()); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }Is there something I am missing that is causing it not to be able to authenticate to the proxy? Some other questions suggested setting -Djdk.http.auth.tunneling.disabledSchemes="" but that seemed to have no effect. Calling the toString() method on the only proxy that the selector can find returns this: HTTP @ /10.10.10.1:8080. Nothing from here seemed to help.
