Online Java Compiler By
JavaTpoint.com
import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class JavaHttpURLConnectiongetHeaderFieldExample2 { //Starting the main method... public static void main(String h[]) throws IOException { //starting the try block try { String link_url1, link_url2; link_url1="https://www.mysql.com"; link_url2="https://www.oracle.com"; URL obj1 = new URL(link_url1); //URL Connection Created... URL obj2 = new URL(link_url2); HttpURLConnection con1 = (HttpURLConnection) obj1.openConnection(); //Http URL Connection Created... HttpURLConnection con2 = (HttpURLConnection) obj2.openConnection(); con1.connect(); System.out.println("www.mysql.com"); System.out.println("Header field 0:" + con1.getHeaderField(0)); System.out.println("Header field 1:" + con1.getHeaderField(1)); System.out.println("www.oracle.com"); System.out.println("Header field 0:" + con2.getHeaderField(0)); System.out.println("Header field 1:" + con2.getHeaderField(1)); HttpURLConnection.setFollowRedirects(true); System.out.println("getFollowRedirects : "+con1.getFollowRedirects()); con1.setConnectTimeout(1000); con1.disconnect(); System.out.println("connection disconnected"); } catch (Exception e) { System.out.println( e); } } }
Output