A Java HTTPS client example

Java HTTPS client FAQ: Can you share some source code for a Java HTTPS client application?

Sure, here's the source code for an example Java HTTPS client program I just used to download the contents of an HTTPS (SSL) URL. I actually found some of this in a newsgroup a while ago, but I can't find the source today to give them credit, so my apologies for that.

I just used this program to troubleshoot a problem with Java and HTTPS URLs, including all that nice Java SSL keystore and cacerts stuff you may run into when working with Java, HTTPS/SSL, and hitting a URL.

This Java program should work if you are hitting an HTTPS URL that has a valid SSL certificate from someone like Verisign or Thawte, but will not work with other SSL certificates unless you go down the Java keystore road.

Example Java HTTPS client program

Here's the source code for my simple Java HTTPS client program:

package foo;

import java.net.URL;
import java.io.*;
import javax.net.ssl.HttpsURLConnection;

public class JavaHttpsExample
  public static void main(String[] args)
  throws Exception
    String httpsURL = "https://your.https.url.here/";
    URL myurl = new URL(httpsURL);
    HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
    InputStream ins = con.getInputStream();
    InputStreamReader isr = new InputStreamReader(ins);
    BufferedReader in = new BufferedReader(isr);

    String inputLine;

    while ((inputLine = in.readLine()) != null)


Just change the URL shown there to the HTTPS URL you want to access, and hopefully everything will work well for you.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.


I try and do this with my URL being google mail:

and I get:
Exception in thread "main" java.io.IOException: Authentication failure
at sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:1329)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:168)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:934)
at com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl.getInputStream(HttpsURLConnectionOldImpl.java:204)
at HTTPSExample.main(HTTPSExample.java:43)

Hmm, I'm still on the road traveling, but I just hit that URL from my laptop here in the hotel and it worked fine. The "Authentication failure" error message makes me wonder if you're going through some sort of proxy, like WebSense?


You're my hero! This works perfectly!



Was using com.sun.net.ssl.HttpsURLConnection which is Deprecated and causing a casting issue.

Your solution worked a treat.



Thank you, man! I was struggling with some other examples I've found, this is just simple and effective for what I need. Keep walking!


You save the world.
Thank you !
This example is useful.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.