Viewing Response Header Lines

This section provides a tutorial Java program, HttpRequestGet.java, to view HTTP response header lines.

When the client program receives the HTTP response, it will look at the header lines first. Based on the information contained in the header lines, the client program will decide what to do with the actual response data in the entity body.

If you use a Web browser as a HTTP client program, it will process the data in the entity body differently depending on mainly the "Content_Type" entity header line: displaying the data as it is, rendering the data as a HTML document and displaying the resulting information, or passing the data to other registered programs to handle it.

Once the Web browser finishes processing the entity body, you can view some limited information from header lines. For example, you can click the right mouse button and select the properties command on Internet Explorer, it will display some general properties about this response in a pop up window. The properties displayed are not always identical to the response header lines. The "Modified" property is probably identical to the "Last_Modified" entity header line. The "Type" property is sometime related to the "Content_Type" entity header line, and sometimes related to server side resource that generated the response. For example, if you use Internet Explorer to request hello.jsp from a JSP Web server, and view the page properties, you will see "JavaServer Page" in the "Type" property. But the "Content_Type" header line received from this JSP page is "text/html".

How to view all the header lines received in the HTTP response? I couldn't find any existing tools to do this. So I wrote the following program to dump the entire response including all header lines received from a Web server:

/**
 * HttpRequestGet.java
 * Copyright (c) 2012, HerongYang.com, All Rights Reserved. 
 */
import java.io.*;
import java.net.*;
public class HttpRequestGet {
   public static void main(String[] args) {
      String path = "/index.html";
      int port = 80;
      String host = "localhost";
      if (args.length > 0) path = args[0];
      if (args.length > 1) port
         = Integer.valueOf(args[1]).intValue();
      if (args.length > 2) host = args[2];
      String result = "";
      try {
         Socket c = new Socket(host,port);
         BufferedWriter w = new BufferedWriter(new OutputStreamWriter(
            c.getOutputStream()));
         BufferedReader r = new BufferedReader(new InputStreamReader(
            c.getInputStream()));
         String m = "GET "+ path + " HTTP/1.0";
         w.write(m,0,m.length());
         w.newLine();
         w.newLine();
         w.flush();
         while ((m=r.readLine())!= null) {
            System.out.println(m);
         }
         w.close();
         r.close();
         c.close();
      } catch (IOException e) {
         System.err.println(e.toString());
      }
   }
}

See next tutorials on how to use this program.

Last update: 2012.

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat 7 Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

 JSP Application Session

 Managing Cookies in JSP Pages

 JavaBean Objects and "useBean" Action Elements

Managing HTTP Response Header Lines

 What Is HTTP Response?

 HTTP Response Header Lines

 Controlling Response Header Lines

Viewing Response Header Lines

 Response Header Lines of Static Files

 Response Header Lines Controlled by "page" Directive

 Response Header Lines Controlled by response Object

 Accessing File System from JSP Pages

 Returning non-HTML Response Body

 Returning Attachments for Web Download

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Outdated Tutorials

 References

 PDF Printing Version