Getting the Response Headers from an HTTP Connection - Java Network

Java examples for Network:Http

Description

Getting the Response Headers from an HTTP Connection

Demo Code

import java.net.URL;
import java.net.URLConnection;

public class Main {

  public void main(String[] argv) {
    try {/*from w  ww .  j  a v  a2 s .  co m*/
      URL url = new URL("http://hostname:80");
      URLConnection conn = url.openConnection();

      // List all the response headers from the server.
      for (int i = 0;; i++) {
        String headerName = conn.getHeaderFieldKey(i);
        String headerValue = conn.getHeaderField(i);

        if (headerName == null && headerValue == null) {
          // No more headers
          break;
        }
        if (headerName == null) {
          // The header value contains the server's HTTP version
        }
      }
    } catch (Exception e) {
    }
  }
}

Related Tutorials