Online Java Compiler By
JavaTpoint.com
import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class JavaHttpURLConnectiongetHeaderFieldExample1 { //Starting the main method... public static void main(String h[]) throws IOException { //starting the try block try { String link_url ; link_url="https://www.javatpoint.com"; URL obj1 = new URL(link_url); //URL Connection Created... HttpURLConnection con1 = (HttpURLConnection) obj1.openConnection(); //Http URL Connection Created... con1.connect(); System.out.println("Header field 0:" + con1.getHeaderField(0)); System.out.println("Header field 1:" + con1.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