The Java HttpURLConnection class is http specific URLConnection. It works for HTTP protocol only.
By the help of HttpURLConnection class, you can information of any HTTP URL such as header information, status code, response code etc.
The java.net.HttpURLConnection is subclass of URLConnection class.
The openConnection() method of URL class returns the object of URLConnection class.
public URLConnection openConnection()throws IOException{}
You can typecast it to HttpURLConnection type as given below.
URL url=new URL("http://www.rookienerd.com/java-tutorial"); HttpURLConnection huc=(HttpURLConnection)url.openConnection();
import java.io.*; import java.net.*; public class HttpURLConnectionDemo{ public static void main(String[] args){ try{ URL url=new URL("http://www.rookienerd.com/java-tutorial"); HttpURLConnection huc=(HttpURLConnection)url.openConnection(); for(int i=1;i<=8;i++){ System.out.println(huc.getHeaderFieldKey(i)+" = "+huc.getHeaderField(i)); } huc.disconnect(); }catch(Exception e){System.out.println(e);} } }