Java URLConnection .getHeaderField (int n)

Syntax

URLConnection.getHeaderField(int n) has the following syntax.

public String getHeaderField(int n)

Example

In the following code shows how to use URLConnection.getHeaderField(int n) method.


import java.net.URL;
import java.net.URLConnection;
//w ww .  j  av a2  s . c  o  m
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;
      }
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.net »




CookieManager
CookiePolicy
CookieStore
DatagramPacket
DatagramSocket
HttpCookie
HttpURLConnection
InetAddress
JarURLConnection
MulticastSocket
ServerSocket
Socket
SocketAddress
URI
URL
URLConnection
URLDecoder
URLEncoder