Online Java Compiler By
JavaTpoint.com
import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; public class JavaHttpURLConnectiongetResponseCodeExample2 { //Starting the main method... public static void main(String h[]) throws IOException { String link_url = "https://www.Facebook.com"; String link_url1 = "https://www.javatpoint.com"; //starting the try block try { URL obj1 = new URL(link_url); //URL Connection Created... HttpURLConnection con1 = (HttpURLConnection) obj1.openConnection(); //Http URL URL obj2 = new URL(link_url1); //URL Connection Created... HttpURLConnection con2 = (HttpURLConnection) obj2.openConnection(); //Http URL int r_code1 =con2.getResponseCode(); int r_code =con1.getResponseCode(); // it is use to get the status code from HTTP response message System.out.println("https://www.Facebook.com"); System.out.println("Connection Response Code : "+HttpURLConnection.HTTP_NOT_AUTHORITATIVE+" " +r_code); System.out.println("https://www.javatpoint.com"); System.out.println("Connection Response Code : "+HttpURLConnection.HTTP_SEE_OTHER+" " +r_code1); con1.disconnect(); con2.disconnect(); System.out.println("Connection Disconnect... "); } catch (Exception e) { System.out.println( e); }}}
Output