HTTP Header

In this chapter you will learn:

  1. How to get HTTP header properties from URLConnection
  2. How to read HTTP connnection header fields
  3. How to get field and get value using a for loop

HTTP header properties from URLConnection

The following code calls the methods from URLConnection to find out the HTTP header properties.

import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
//from  j av a2s  .c o m
public class Main {

  public static void main(String args[]) throws Exception {

    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();
    System.out.println("Content-type: " + uc.getContentType());
    System.out.println("Content-encoding: " + uc.getContentEncoding());
    System.out.println("Date: " + new Date(uc.getDate()));
    System.out.println("Last modified: " + new Date(uc.getLastModified()));
    System.out.println("Expiration date: " + new Date(uc.getExpiration()));
    System.out.println("Content-length: " + uc.getContentLength());
  }

}

The code above generates the following result.

Get HTTP connnection header fields

The following code reads out the http header. It creates a URL pointing to java2s.com frist. And then create URLConnection from the URL. After that it calls the getHeaderFields() method. getHeaderFields() method returns a map. In that map the key is the HTTP header key and the value is a List<String> which is the HTTP header value.

import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.Set;
/*from  ja va 2  s  . c  o m*/
public class Main {
    public static void main(String[] args) throws Exception {
        URL url = new URL("http://www.java2s.com/");
        URLConnection urlConnection = url.openConnection();
        Map<String, List<String>> headers = urlConnection.getHeaderFields();
        Set<Map.Entry<String, List<String>>> entrySet = headers.entrySet();
        for (Map.Entry<String, List<String>> entry : entrySet) {
            String headerName = entry.getKey();
            System.out.println("Header Name:" + headerName);
            List<String> headerValues = entry.getValue();
            for (String value : headerValues) {
                System.out.print("Header value:" + value);
            }
            System.out.println();
        }
    }
}

The code above generates the following result.

The following code uses Iterator to get the HTTP header values.

import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/*from  j  a v a2s .  c o  m*/
public class Main {
  public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/index.html");
    URLConnection connection = url.openConnection();

    Map responseMap = connection.getHeaderFields();
    for (Iterator iterator = responseMap.keySet().iterator(); iterator.hasNext();) {
      String key = (String) iterator.next();
      System.out.println(key + " = ");

      List values = (List) responseMap.get(key);
      for (int i = 0; i < values.size(); i++) {
        Object o = values.get(i);
        System.out.println(o + ", ");
      }
    }
  }
}

The code above generates the following result.

Get field and get value

The following code finds out the HTTP header by calling getHeaderFieldKey and getHeaderField.

import java.net.URL;
import java.net.URLConnection;
//from  j  ava  2s  .com
public class Main {
  public static void main(String[] argv) throws Exception {
    URL url = new URL("http://java2s.com");
    URLConnection conn = url.openConnection();

    for (int i = 0;; i++) {
      String headerName = conn.getHeaderFieldKey(i);
      String headerValue = conn.getHeaderField(i);
      System.out.println(headerName);
      System.out.println(headerValue);

      if (headerName == null && headerValue == null) {
        System.out.println("No more headers");
        break;
      }
    }
  }
}

Next chapter...

What you will learn in the next chapter:

  1. How to post to URLConnection
Home » Java Tutorial » URL URI
URL
URL Creation
URL for jar file
URL Components
Convert file path to URL
URL Relative
URL Protocol
Read from URL
Compare URL
URLConnection
HTTP Header
URLConnection Post
Cookie
URLConnection Read
HttpURLConnection
HttpURLConnection Properties
HttpURLConnection proxy
HttpURLConnection Authenticator
HTTPS
JarURLConnection
Encode a URL
Decode a URL
URI
URI Normalization
URI Resolution
URI Relativization
Convert URI to URL
IP Address
IP Ping
NetworkInterface