0

I'm having issues retrieving the content from some HTTPS sites using the Java 11 HTTPClient. Here's the code I'm using:

HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
  .uri(URI.create("https://en.wikipedia.org/wiki/Main_Page"))
  .build();

HttpResponse<byte[]> response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
System.out.println(response.statusCode());
System.out.println(new String(response.body()));

When trying to download the source of Wikipedia, I get the following error:

java.io.IOException: Received fatal alert: handshake_failure

If I use the same code but change the source URL to https://www.google.com, it works fine. From a bit of Googling, this makes it sound to me like it's probably an issue with differing crypto algorithms (although I don't know that), but it's not clear to me how to fix it. I've tried a few solutions that have been suggested on other StackOverflow questions, but haven't found one that works for me or a reliable guide to debugging the problem.

Can someone please point me in the direction of what I can try to fix the issue?

$ java -version
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)
James Baker
  • 1,143
  • 17
  • 39
  • Have you tried changing it to http? – GamingFelix May 05 '21 at 11:40
  • Doing so returns a 301 status code (redirect), to the HTTPS version. So I either don't get any content besides the redirect headers, or I get the error above if I follow the redirect. – James Baker May 05 '21 at 12:39
  • When I googled it, I found something about the security. Maybe there's a way you can turn it off on the httpClient? – GamingFelix May 05 '21 at 12:43
  • Cannot reproduce. Just copy-pasted this code in `OpenJDK 11` environment, and it works fine. Anything related to the network filtering? firewall?.. – Giorgi Tsiklauri May 05 '21 at 12:44
  • i am not getting any issue with that version, it is working fine in my mac with same version – Mahamudul Hasan May 05 '21 at 12:54
  • Thanks for confirming it's my environment rather than the code. The computer isn't behind anything fancy in terms of firewalls, and accessing the URL directly in a browser works fine. I'll try reinstalling my Java environment and see if that works. – James Baker May 05 '21 at 13:09
  • Reinstalling Java, as well as trying different versions, hasn't helped - does anyone know what might be the issue? – James Baker May 06 '21 at 06:57
  • I'd suggest to set `-Djdk.httpclient.HttpClient.log=errors,requests,headers,ssl` on the java command line. If that doesn't provide enough clue you could also try to add `-Djavax.net.debug=all` – daniel May 06 '21 at 18:25

1 Answers1

0

I don't know what caused problem on your environment, but just to test the URL I ran a different Http client using Java 8, and it worked just fine. Just in case you would want to try this Http client Here is the code and the info where to get the client:

    private static void testHttpClient() {
        HttpClient client = new HttpClient();
        client.setConnectionUrl("https://en.wikipedia.org/wiki/Main_Page");
        String result;
        try {
            ByteBuffer buff = client.sendHttpRequestForBinaryResponse(HttpMethod.GET);
            result = new String(buff.array());
//          result = client.sendHttpRequest(HttpMethod.GET);
            System.out.println(result);
        } catch (IOException e) {
            result = TextUtils.getStacktrace(e, "com.mot.");
        }
    }

This code worked with both options: you can comment the two lines

            ByteBuffer buff = client.sendHttpRequestForBinaryResponse(HttpMethod.GET);
            result = new String(buff.array());

and uncomment the line

//          result = client.sendHttpRequest(HttpMethod.GET);

And it also works. The HttpClient used here comes from MgntUtils Open Source library. That library is available as Maven Artifacts Maven Central here And also on GitHub (including source code and Javadoc). Javadoc for The library could be seen here.
Disclaimer: The library is written by me

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36