Getting the Response Headers from an HTTP Connection : URLConnection « Network « Java Tutorial






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

public class Main {
  public static void main(String[] argv) throws Exception {
    URL url = new URL("http://hostname:80");
    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;
      }
    }
  }
}








19.5.URLConnection
19.5.1.java.net.URLConnection
19.5.2.URLConnection.openStream is more powerful than URL.openStream
19.5.3.Header Viewer
19.5.4.Sending a POST Request with Parameters From a Java Class
19.5.5.Downloading a web page using URL and URLConnection classes
19.5.6.Get response header from HTTP request
19.5.7.Getting the Response Headers from an HTTP Connection
19.5.8.Getting the Cookies from an HTTP Connection
19.5.9.Preventing Automatic Redirects in a HTTP Connection
19.5.10.Sending a Cookie to an HTTP Server
19.5.11.Writing to a Web server
19.5.12.Identify yourself using HTTP Authentification