7

I'm trying to download some content using the URL class with a given link that comes from the server.

My code to download is it:

            URL url = new URL(downloadUrl);
            InputStream stream = url.openStream();
            byte[] content = new byte[stream.available()];
            stream.read(content);
            stream.close();

But when running I got the following exception:

 java.io.IOException: SSL handshake failure: Failure in SSL library, usually a protocol error
 error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:604 0xaf076228:0x00000000)
     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.nativeconnect(Native Method)
 ...

The link I was using is something like:

https://contentserver.com/d/761/34/215656/5de1a41ea3bc9c81978af95ed19b03286f64d9a3

I know if I enter it on browser it donwload an File, I want download the same file throught Java.

Thanks

2
  • Are you using a self signed certificate? Android does not play well with those. Commented Mar 9, 2011 at 22:00
  • Hmm.. actually, I don't know it. I think I should talk with the developers of the server side to know it. Commented Mar 10, 2011 at 13:24

3 Answers 3

1
+50

Code to read data from the https url in java

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.*;
import java.security.Security;
import java.util.Properties;

public class UseHttps {

public static void main(String argv[]) {

String fullURL = "https://fortress.wa.gov/lni/bbip/detail.aspx?License=SIBLUCL004C5";
try {

    URL page = new URL(fullURL); // Process the URL far enough to find the right handler
    URLConnection urlc = page.openConnection();
    urlc.setUseCaches(false); // Don't look at possibly cached data
    System.out.println("Content-type = " + urlc.getContentType()); // See what's here
    System.out.println("Content-length = " + urlc.getContentLength()); // See how much of it there is
    // Read it all and print it out
    BufferedReader br = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
    String buffer = "";
    while (buffer != null) {
        try {
            System.out.println(buffer);
            buffer = br.readLine();
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
            break;
        }
    }
}
catch (MalformedURLException mue) {
System.out.println(fullURL + " is not a URL that can be resolved");
}
catch (IOException ie) {
ie.printStackTrace();
}
}
}
Sign up to request clarification or add additional context in comments.

Comments

0

Marcos,

this might be completely irrelevant, but... I was getting the same error...

abort: error: _ssl.c:490: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

... trying to do an "hg clone" on my bitbucket.org mercurial repo..., I was trying to connect to bitbucket over https from the windows command line... it turned out that I was supplying the incorrect password to my proxy...

Comments

0

While I am not sure about your SSL error, the way you are reading data is most likely NOT what you want. InputStream.available() is not the amount of data in the stream. The stream is by definition "unbounded", and it is over only when it is over. InputStream does not know how many bytes it has. Method available() simply tells you how many bytes can be read without blocking on IO.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.