1

I'm making HTTP request to a https website using Unirest for Java, but I have problem with SSL certificate. Exception message - javax.net.ssl.SSLException: hostname in certificate didn't match:

Page: www.ceskereality.cz

If I open it in browser (I'm using Google Chrome on Windows 10), it works...

I tried this, but it isn't working

SSLContext sslcontext = SSLContexts.custom()
                .loadTrustMaterial(null, new TrustSelfSignedStrategy())
                .build();

SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom()
                .setSSLSocketFactory(sslsf)
                .build();
Unirest.setHttpClient(httpclient);
Erik Bystroň
  • 95
  • 4
  • 11
  • 1
    Possible duplicate of [Java SSLException: hostname in certificate didn't match](https://stackoverflow.com/questions/7256955/java-sslexception-hostname-in-certificate-didnt-match) – MWiesner Dec 20 '18 at 14:56
  • Problem is that if I open that page in browser it works... (question edited) – Erik Bystroň Dec 20 '18 at 15:09

2 Answers2

0

Download the server cert and place it in your cacerts and restart your java application.

Feras Wilson
  • 380
  • 1
  • 3
  • 13
0

The server requires that the client uses Server Name Indication (SNI) in order to get the correct certificate. If no SNI is used a certificate for ci.cz instead of ceskereality.cz is returned which causes the error you see.

I'm not familiar with Unirest but it is likely that the library you use does not support SNI or that you are using it with an older Java version which does not have SNI enabled.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172