Java HttpURLConnection class

Java HttpURLConnection class

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.

How to get the object of HttpURLConnection class

The openConnection() method of URL class returns the object of URLConnection class. Syntax:

  1. public URLConnection openConnection()throws IOException{}

You can typecast it to HttpURLConnection type as given below.

  1. URL url=new URL(“http://www.javatpoint.com/java-tutorial”);
  2. HttpURLConnection huc=(HttpURLConnection)url.openConnection();

Java HttpURLConnecton Example

  1. import java.io.*;
  2. import java.net.*;
  3. public class HttpURLConnectionDemo{
  4. public static void main(String[] args){
  5. try{
  6. URL url=new URL(“http://www.javatpoint.com/java-tutorial”);
  7. HttpURLConnection huc=(HttpURLConnection)url.openConnection();
  8. for(int i=1;i<=8;i++){
  9. System.out.println(huc.getHeaderFieldKey(i)+” = “+huc.getHeaderField(i));
  10. }
  11. huc.disconnect();
  12. }catch(Exception e){System.out.println(e);}
  13. }
  14. }

Test it Now

Output:

Date = Wed, 10 Dec 2014 19:31:14 GMT
Set-Cookie = JSESSIONID=D70B87DBB832820CACA5998C90939D48; Path=/
Content-Type = text/html
Cache-Control = max-age=2592000
Expires = Fri, 09 Jan 2015 19:31:14 GMT
Vary = Accept-Encoding,User-Agent
Connection = close
Transfer-Encoding = chunked
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Java HttpURLConnection class 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. How to get the object of HttpURLConnection class The openConnection() method […]

Tags:

....
...