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 getHeaderFields() Method



Description

The Java URLConnection getHeaderFields() method returns an unmodifiable Map of the header fields. The Map keys are Strings that represent the response-header field names. Each Map value is an unmodifiable List of Strings that represents the corresponding field values.

Declaration

Following is the declaration for java.net.URLConnection.getHeaderFields() method

public Map<String,List<String>> a Map of header fields()

Parameters

NA

Return Value

a Map of header fields

Exception

NA

Example 1

The following example shows the usage of Java URLConnection getHeaderFields() 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 getHeaderFields(), 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();
         System.out.println(urlConnection.getHeaderFields());
      } 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], X-Cache=[HIT], Server=[ECAcc (ndl/D383)], Access-Control-Allow-Origin=[*;, *], X-Version=[OCT-10 V1], Last-Modified=[Wed, 06 Dec 2023 19:59:35 GMT], Date=[Thu, 07 Dec 2023 06:30:20 GMT], X-Frame-Options=[SAMEORIGIN], Cache-Control=[max-age=2592000], Vary=[Accept-Encoding], Expires=[Sat, 06 Jan 2024 06:30:20 GMT], Content-Length=[293827], X-XSS-Protection=[1; mode=block], Age=[37846], Content-Type=[text/html; charset=UTF-8]}

Example 2

The following example shows the usage of Java URLConnection getHeaderFields() 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 getHeaderFields(), 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("http://www.tutorialspoint.com");
         URLConnection urlConnection = url.openConnection();
         System.out.println(urlConnection.getHeaderFields());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

{null=[HTTP/1.1 302 Found], Server=[Apache/2.4.52 (Ubuntu)], Access-Control-Allow-Origin=[*;, *], X-Version=[OCT-10 V1], Date=[Thu, 07 Dec 2023 06:30:51 GMT], X-Frame-Options=[SAMEORIGIN], Cache-Control=[max-age=2592000], Vary=[User-Agent], Expires=[Sat, 06 Jan 2024 06:30:51 GMT], Content-Length=[0], X-XSS-Protection=[1; mode=block], Location=[https://www.tutorialspoint.com/index.htm], Content-Type=[text/html; charset=UTF-8]}

Example 3

The following example shows the usage of Java URLConnection getHeaderFields() 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 getHeaderFields(), 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("http://www.google.com");
         URLConnection urlConnection = url.openConnection();
         System.out.println(urlConnection.getHeaderFields());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

{Transfer-Encoding=[chunked], null=[HTTP/1.1 200 OK], Server=[gws], P3P=[CP="This is not a P3P policy! See g.co/p3phelp for more info."], Date=[Thu, 07 Dec 2023 06:32:15 GMT], Accept-Ranges=[none], X-Frame-Options=[SAMEORIGIN], Cache-Control=[private, max-age=0], Vary=[Accept-Encoding], Set-Cookie=[NID=511=862wb45sRozZXdCW9PbBoyN3ohbb0moh4Glc2tYKpw338yz_b_8IvdjPQGUA; expires=Fri, 07-Jun-2024 06:32:15 GMT; path=/; domain=.google.com; HttpOnly, AEC=Q; expires=Tue, 04-Jun-2024 06:32:15 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax, 1P_JAR=2023-12-07-06; expires=Sat, 06-Jan-2024 06:32:15 GMT; path=/; domain=.google.com; Secure], Expires=[-1], X-XSS-Protection=[0], Content-Security-Policy-Report-Only=[object-src 'none';base-uri 'self';script-src 'nonce-Ne6LTQm5GlkKZNU29HBO6A' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/gws/other-hp], Content-Type=[text/html; charset=ISO-8859-1]}
java_urlconnection.htm
Advertisements