Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Network GetHTTPHeader

Related Tutorials


 
 

Share on Google+Share on Google+

GetHTTPHeader

Advertisement
In this section, you will learn how to get content-length, content- type, and last-modify date of a file. Here we provide a complete example that uses getHeaderFieldKey() method. Firstly create a class GetHTTPHeader and initialize a class URLConnection. T

GetHTTPHeader

     

In this section, you will learn how to get content-length, content- type, and last-modify date of a file. Here we provide a complete example that uses getHeaderFieldKey() method. Firstly create a class GetHTTPHeader and initialize a class URLConnection. Then we call openConnection() method. After that we use the try and catch block to know whether the code is working correctly or not. 

This is the method returns the key for the
nth header field. The HTTP server returns the status line. In this case, getHeaderField() method returns the status line, but getHeaderFieldKey() method returns null.


Here is the code:

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

public class GetHTTPHeader{
  public static void main (String[] args) throws IOException {
    try {
      URL url = new URL
("file:///C:/Documents%20and%20Settings/comp23/Desktop/AnimationLine.html");
      System.out.println(url);
      URLConnection con = url.openConnection();
      for (int i=0; i< 10; i++) {
        String name = con.getHeaderFieldKey(i);
        String value = con.getHeaderField(i);
        if (name == null && value == null){
          break;         
        }
        else{
          System.out.println(name + "=" + value);
        }
      }
    }
    catch (Exception e) {}
  }
} 


Here is the code of this program:

C:\rose>java GetHTTPHeader
file:/C:/Documents%20and%20Settings/comp23/Desktop/AnimationLine.html
content-length=110
last-modified=Fri, 15 Jun 2007 12:02:17 GMT

C:\rose>

 

Output of this program

 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 19, 2008

Related Tutorials

Discuss: GetHTTPHeader  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com