How to connect to an HTTPS page in java using HttpsURLConnection

The folllowing code will connect to a https page (using SSL), get the response code and content.

 

If you have timeout or networking issues, check if you don’t have to modify the firewall configuration or install a SSL certificate on your JVM.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public class SampleHttpsClient {
 
 public static void main(String[] args) {
  
  String host = "https://www.google.com";
  try {
   
   URL url = new URL(host);
   HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
   
   // get response code, 200 = Success
   int responseCode = httpsURLConnection.getResponseCode();
   System.out.println(responseCode);
   
   // if response == 200, display the content
   if(responseCode==200){
    System.out.println(httpsURLConnection.getContent().toString());
    BufferedReader br = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream()));
    String line=null;
    while((line = br.readLine())!= null){
     System.out.println(line);
    }
   }
   
  } catch (IOException e) {
   e.printStackTrace();
  } 
  
 }
 
}

 

Java httpURLConnection example

This code will connect to a given URL and gets the content if the response is a success.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpClient {
  
  private static final String USER_AGENT = "Mozilla/5.0";
  
  public static void main(String[] args) {
    
    String host = "http://tutoref.com";
    try {
      
      URL url = new URL(host);
      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
      
      // specify request header
      httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
      
      // get response code, 200 = Success
      int responseCode = httpURLConnection.getResponseCode();
      System.out.println(responseCode);
      
      // if response == 200, display the content
      if(responseCode==200){
        System.out.println(httpURLConnection.getContent().toString());
        BufferedReader br = 
           new BufferedReader(
                        new InputStreamReader(httpURLConnection.getInputStream())
           );
        String line=null;
        while((line = br.readLine())!= null){
          System.out.println(line);
        }
      }
      
      
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

}

 

 

HttpURLConnection example

HttpURLConnection allows to open an HTTP connection and get a response.

URL url = new URL("http://www.tutoref.com");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
int statusCode = http.getResponseCode();
// if statusCode == 200, it means it's ok.
// other ways, it's an error you can deal with it.