import java.io.IOException;
import java.net.*;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Base64;

public class Main {
    public static void main(String[] args) throws IOException, InterruptedException {
        // please input actual proxy server's info
        String proxyIP = "1.1.1.1";
        int proxyPort = 8899;
        String proxyUsername = "proxyUsername", proxyPassword = "proxyPassword";

// enable basic authorization
        System.setProperty("jdk.http.auth.proxying.disabledSchemes", "");
        System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");

        HttpClient client = HttpClient.newBuilder()
                .proxy(ProxySelector.of(new InetSocketAddress(proxyIP, proxyPort)))
                .authenticator(new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        if (getRequestorType() != RequestorType.PROXY) {
                            return null;
                        }
                        return new PasswordAuthentication(proxyUsername, proxyPassword.toCharArray());
                    }
                })
                .build();
        HttpResponse<String> response = client.send(HttpRequest.newBuilder(URI.create("xxxxxxxx"))
                .setHeader("Authorization", getBasicAuthenticationHeader("foo", "bar"))
                .build(), HttpResponse.BodyHandlers.ofString());
        System.out.println(response.statusCode());
        System.out.println(response.body());
    }

    private static String getBasicAuthenticationHeader(String username, String password) {
        String valueToEncode = username + ":" + password;
        return "Basic " + Base64.getEncoder().encodeToString(valueToEncode.getBytes());
    }
}