How To Get Response Headers From a HTTP Request in Java

With this Java program you can get the the response headers from a HTTP request. You may have seen HTTP headers in Google Chrome/Mozilla Firefox network feature in inspect element (it just automatically captures HTTP).

All you have to do is run the program from command-line or on eclipse (or any IDE you want), after executing the program you will be asked to enter an URL to get the response headers from HTTP request (Note : make sure you don't enter URL's without 'http://'). Within seconds you will get the HTTP request response headers one after the next line.

This program has 7 namespaces, of course you need them in order to compile/execute the program
import java.util.Scanner;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

and we are using a public string that helps to make a line break
public static String BR = System.getProperty("line.separator");

Okay let me put all the parts together, it uses Scanner(that to get the users input), Map, List, Set, Iterator, a while loop, String Builder and finally it prints.

Program Source Code
import java.util.Scanner;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/* 
Author : Mohamed Shimran
Description : Getting Response Headers From a HTTP Request
Blog : http://ultimateprogrammingtutorials.info
Facebook : http://fb.me/54j33dh4
Twitter : http://twitter.com/54j33dh4
 */
 
public class httpRequest {
public static String BR = System.getProperty("line.separator");
public static void main(String[] args) throws Exception {

  String Link;
     Scanner in = new Scanner(System. in );
     System.out.println("Enter Link To Get Response Headers From HTTP Request :");
     Link = in .nextLine();
   
System.out.println(BR);

 URL url = new URL(Link);
 URLConnection conn = url.openConnection();
 
 Map> HF = conn.getHeaderFields();
 Set HFS = HF.keySet();
 Iterator HFI = HFS.iterator();
 
 while (HFI.hasNext()) {

      String HFK = HFI.next();
      List HFV = HF.get(HFK);

      StringBuilder strbld = new StringBuilder();
      for (String value : HFV) {
   strbld.append(value);
   strbld.append("");
   }
      System.out.println(HFK + "=" + strbld.toString());
   }
   }
}

Process
javac httpRequest.java

java httpRequest
Enter Link To Get Response Headers From HTTP Request :
http://www.ultimateprogrammingtutorials.info

null=HTTP/1.1 200 OK
ETag="5064ad83-05c5-4cef-bbba-a3030b0727da"
Transfer-Encoding=chunked
Date=Wed, 15 Jan 2014 14:12:06 GMT
X-XSS-Protection=1; mode=block
Expires=Wed, 15 Jan 2014 14:12:06 GMT
Alternate-Protocol=80:quic,80:quic
Last-Modified=Wed, 15 Jan 2014 14:10:59 GMT
Content-Type=text/html; charset=UTF-8
Server=GSE
X-Content-Type-Options=nosniff
Cache-Control=private, max-age=0



How To Get Response Headers From a HTTP Request in Java

Final Words
What can I say? It's just another simple java program by me(of course got help from many resources). I hope you really like it or found it useful, share it if it deserves and PEACE!.

3 comments

I am not a lot into "technical stuff" of blogging and I hate when I need to code or modify the CSS.
But this kind of article surely is a useful guide.
Thanks for sharing at Klinkk, Mohamed!

Reply

The increase typing skills is such a crucial part of ourmodern lifestyle. From this site we can help you get your speed wherever you want it to be. It’s easier than you might think!

Reply

Post a Comment

Note: Only a member of this blog may post a comment.