Online Java Compiler By
JavaTpoint.com
import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; public class JavaHttpURLConnectiongetResponseCodeExample1 { //Starting the main method... public static void main(String h[]) throws IOException { //starting the try block try { String link_url = "https://www.mysql.com"; int i; i=10; URL obj1 = new URL(link_url); //URL Connection Created... HttpURLConnection con1 = (HttpURLConnection) obj1.openConnection(); //Http URL Connection Created... int r_code =con1.getResponseCode(); // it is use to get the status code from HTTP response message System.out.println("https://www.mysql.com"); System.out.println("Connection Response Code : "+r_code); if (r_code != HttpURLConnection.HTTP_OK) // HTTP_OK : HTTP Status-Code 200: OK. { System.out.println("Download failed" + r_code ); System.exit(0); } con1.disconnect(); System.out.println("Connection Disconnect... "); } catch (Exception e) { System.out.println( e); } } }
Output