0

I am using the code below to download an image that's only available through HTTPS. The cert is expired, so if I want to download the image, I have to manually click the ignore cert error in the browser. How can I do the same in my code?

URL website = new URL("https://domain/image.jpg");    
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("C:\\temp\\image.jpg");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();

Here's the exception I am getting with this code:

Exception in thread "main" javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at java.net.URL.openStream(Unknown Source)
    at DownloadImage.main(DownloadImage.java:17)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
    at sun.security.ssl.InputRecord.read(Unknown Source)
Scope
  • 49
  • 7
  • 1
    Possible duplicate of [How to bypass ssl certificate checking in java](https://stackoverflow.com/questions/42806709/how-to-bypass-ssl-certificate-checking-in-java) – luk2302 Aug 01 '19 at 16:15
  • According to the exception, the other side closed the connection. This isn't necessarily a certificate error. – Mark Rotteveel Aug 02 '19 at 18:05

0 Answers0