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. 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. T

GetHTTPHeader

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