Search code examples
javahttpsapache-commons

Do the Java platform libraries support https


All I need to to do is to connect via https. Must I use commons client for this?


Solution

  • No, you don't have to, you can use a regular URLConnection. Something like this:

    public class URLConnectionReader {
    
        public static void main(String[] args) throws Exception {
            URL url = new URL("https://jax-ws.dev.java.net/");
            URLConnection uc = url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                                    uc.getInputStream()));
            String inputLine;
    
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
            }
            in.close();
        }    
    }
    

    This may require a bit more work if the site you're connecting to uses a certificate that has not been signed by a well known CA or a self-signed certificate. But this is another story.