javahttpunirest

407 Proxy Authentication error - Unirest Java 8


Hi everyone i'm so hopeless so ask you guys. I'm trying to do a simple HTTP request but with my proxies I get 407 error code. I use Unirest and Java 8.

 Unirest.config().proxy(host, port, usernameProxy, passwordProxy);   
 HttpResponse<JsonNode> response = Unirest.post(url).asJson();
 String body = response.getBody().toString();

That's it, my url is private but i wrote it like this: "https://myurl.com/?param1=param&param2.... It works proxyless but i'm stuck with proxies. Thanks a lot


Solution

  • Seems like the proxy server expects for the proxy credentials within the Headers, which Unirest doesn't seem to propagate.

    The header must specifically contain the "Proxy-Authorization" key in order to the handshake be even started.

    String proxyCred= "user:password"; 
    String baseCred= Base64.getEncoder().encodeToString(proxyCred.getBytes());
    HttpHeaders headers = new HttpHeaders();
    headers.add("Proxy-Authorization", "Basic " + baseCred); // the proxy server needs this
    

    This solution uses the Basic mechanism; It may not work, as the proxy may expect another type of authentication. You'll know which one is by reading the Proxy-Authenticate header within the server's response.

    If the communication is not secured (HTTP and not HTTPS), you could read the response by sniffing the packet with some tool such as WireShark. Once you locate the 407 packet, you could read inside the Proxy-Authenticate value and modify your authorization method according do it.