Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - URLConnection getHeaderField(int n) Method



Description

The Java URLConnection getHeaderField(int n) method returns the value for the nth header field. It returns null if there are fewer than n+1 fields. This method can be used in conjunction with the getHeaderFieldKey method to iterate through all the headers in the message.

Declaration

Following is the declaration for java.net.URLConnection.getHeaderField(int n) method

public String getHeaderField(int n)

Parameters

n − an index, where n>=0

Return Value

the value of the nth header field or null if there are fewer than n+1 fields

Exception

NA

Example 1

The following example shows the usage of Java URLConnection getHeaderField(int n) method for a valid url with https protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getHeaderField(), we're getting the value of all header fields of URLConnection instance and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com");
         URLConnection urlConnection = url.openConnection();
         for (int i = 0;; i++) {
            String headerName = urlConnection.getHeaderFieldKey(i);
            String headerValue = urlConnection.getHeaderField(i);
            if (headerName == null && headerValue == null) {
               break;
            }
            System.out.println(headerName + ":" + headerValue);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

null:HTTP/1.1 200 OK
Access-Control-Allow-Origin:*
Access-Control-Allow-Origin:*;
Age:165900
Cache-Control:max-age=2592000
Content-Type:text/html; charset=UTF-8
Date:Wed, 06 Dec 2023 05:11:49 GMT
Expires:Fri, 05 Jan 2024 05:11:49 GMT
Last-Modified:Mon, 04 Dec 2023 07:06:49 GMT
Server:ECAcc (ndl/D383)
Vary:Accept-Encoding
X-Cache:HIT
X-Frame-Options:SAMEORIGIN
X-Version:OCT-10 V1
X-XSS-Protection:1; mode=block
Content-Length:293801

Example 2

The following example shows the usage of Java URLConnection getHeaderField(int n) method for a valid url with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getHeaderField(), we're getting the value of all header fields of URLConnection instance and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com");
         URLConnection urlConnection = url.openConnection();
         for (int i = 0;; i++) {
            String headerName = urlConnection.getHeaderFieldKey(i);
            String headerValue = urlConnection.getHeaderField(i);
            if (headerName == null && headerValue == null) {
               break;
            }
            System.out.println(headerName + ":" + headerValue);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

null:HTTP/1.1 200 OK
Access-Control-Allow-Origin:*
Access-Control-Allow-Origin:*;
Age:166167
Cache-Control:max-age=2592000
Content-Type:text/html; charset=UTF-8
Date:Wed, 06 Dec 2023 05:16:16 GMT
Expires:Fri, 05 Jan 2024 05:16:16 GMT
Last-Modified:Mon, 04 Dec 2023 07:06:49 GMT
Server:ECAcc (ndl/D383)
Vary:Accept-Encoding
X-Cache:HIT
X-Frame-Options:SAMEORIGIN
X-Version:OCT-10 V1
X-XSS-Protection:1; mode=block
Content-Length:293801

Example 3

The following example shows the usage of Java URLConnection getHeaderField(int n) method for a valid url with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getHeaderField(), we're getting the value of all header fields of URLConnection instance and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.time.Instant;
import java.util.Date;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.google.com");
         URLConnection urlConnection = url.openConnection();
         for (int i = 0;; i++) {
            String headerName = urlConnection.getHeaderFieldKey(i);
            String headerValue = urlConnection.getHeaderField(i);
            if (headerName == null && headerValue == null) {
               break;
            }
            System.out.println(headerName + ":" + headerValue);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

null:HTTP/1.1 200 OK
Date:Wed, 06 Dec 2023 05:13:35 GMT
Expires:-1
Cache-Control:private, max-age=0
Content-Type:text/html; charset=ISO-8859-1
Content-Security-Policy-Report-Only:object-src 'none';...
P3P:CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server:gws
X-XSS-Protection:0
X-Frame-Options:SAMEORIGIN
Set-Cookie:1P_JAR=2023-12-06-05; expires=Fri, 05-Jan-2024 05:13:35 GMT;...
Set-Cookie:AEC=Ackid1S2Iur-...HttpOnly; SameSite=lax
Set-Cookie:...
Accept-Ranges:none
Vary:Accept-Encoding
Transfer-Encoding:chunked
java_urlconnection.htm
Advertisements