0

I am having problems using HTTPS in my new URL it causes it to fail whenever its https :S how can I use a https url in my java code

        ByteArrayOutputStream output = new ByteArrayOutputStream();
        URL urla = new URL(location);
        InputStream openStream = urla.openStream();
        byte[] buffer = new byte[ 1024 ];
        int sizer = 0;

        while( (size = openStream.read( buffer ) ) != -1 ) {
            output.write( buffer, 0, sizer );
        }

my script automatically gives a location but it has to be a https

I can open the url's on my browser (I have a log.d on which tells me the url)

arberb
  • 960
  • 3
  • 14
  • 25

2 Answers2

0

Refer to and download Apache HttpComponents and use that library to do your connection.

For a more advanced example, take a look at my example of a HTTPS POST upload request.

djangofan
  • 28,471
  • 61
  • 196
  • 289
0

Where does location come from? Whereever it comes from, make it emit an HTTPS URL instead of HTTP.

Also, before you try, make sure that the Web server actually supports HTTPS. HTTPS is never enabled by default, and depending on your hosting/setup, enabling HTTPS might be tricky.

EDIT re: comment: replace with

URL urla = new URL("https://" + location); 

and have fun. But still, do check if the HTTPS URL is valid.

EDIT: your HTTP download code is wrong. Here's how an HTTP download looks on Android:

HttpClient hc = new DefaultHttpClient();
URL urla = new URL(location);
HttpResponse Resp = hc.execute(new HttpGet(urla));
//Check for HTTP errors here
InputStream openStream = Resp.getEntity().getContent();
byte[] buffer = new byte[ 1024 ];          
int sizer = 0;            
while( (size = openStream.read( buffer ) ) != -1 )
{             
    output.write( buffer, 0, sizer );          
}  

And you better do this on a background thread. Blocking the UI thread with a HTTP request is a bad idea.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • the location is generated bny a string. The site has https enabled. It emitts a "https://STRING/STRING2?STRING3" like url – arberb Jan 04 '12 at 19:15
  • I tried that but it still fails. The location part already has the HTTPS:// in it. – arberb Jan 04 '12 at 20:29
  • When I check the url validity it says "The certificate should be trusted by all major web browsers (all the correct intermediate certificates are installed)." & "The hostname (*) is correctly listed in the certificate." – arberb Jan 04 '12 at 20:29
  • Where'd the SSL certificate come from? Commercial authority? Or self-signed? – Seva Alekseyev Jan 04 '12 at 20:40
  • I believe its self signed and that code still doesnt work I get "REASON: javax.net.ssl.SSLPeerUnverifiedException: No peer certificate" – arberb Jan 04 '12 at 20:55
  • I tried that but I get REASON: java.lang.ArrayIndexOutOfBoundsException – arberb Jan 04 '12 at 23:15
  • Finally figured it out the arrayindexoutofobounds is from my InputStream openStream = Resp.getEntity().getContent(); .. I changed it to BufferedReader reader = null; StringBuilder builder = new StringBuilder(); try { reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); for (String line; (line = reader.readLine()) != null;) { builder.append(line.trim()); } } finally { if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {} } – arberb Jan 05 '12 at 18:24