Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Java get Http Headers 
 

In this section, we are going to illustrate you how to get the Http Headers. Here, we are using the URL class which points the resource.

 

Java get Http Headers

                         

In this section, we are going to illustrate you how to get the Http Headers. Here, we are using the URL class which points the resource. In order to make a communication link between the application and a URL, we have used the class URLConnection.

url.openConnection()-  This method of URL class returns a URLConnection object that represents a connection referred to by the URL.

conn.getHeaderFieldKey(i)-  This method returns the key for the header field.

conn.getHeaderField(i)- This method returns the value for the header field.

Here is the code of GetHttpHeaders.java

import java.net.*;
import java.io.*;

public class GetHttpHeaders{
  public static void main ( String[] args ) throws IOException {
    try 
    {    
      URL url = new URL("http://localhost:8080");
      URLConnection conn = url.openConnection();
    
      for (int i=0; ; i++) 
      {
            String name = conn.getHeaderFieldKey(i);
            String value = conn.getHeaderField(i);
            if (name == null && value == null){
              break;         
            }
            if (name == null){
              System.out.println("Server HTTP version, Response code:");
              System.out.println(value);
              System.out.print("\n");
            }
            else{
              System.out.println(name + "=" + value);
            }
      }
    
    catch (Exception e) {}
  }
}

To run the above example, first of all start the tomcat as you have taken the resource http://localhost:8080. Then compile the class and execute the example, you will get the Http Headers on the console.

Download Source Code

                         

» View all related tutorials
Related Tags: c class ant help property get tutorial name ria this for to e des pe in as sta m nt

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.