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(String name)



Description

The Java URLConnection getHeaderField(String name) method returns the value of the named header field. If called on a connection that sets the same header multiple times with possibly different values, only the last value is returned.

Declaration

Following is the declaration for java.net.URLConnection.getHeaderField(String name) method

public String getHeaderField(String name)

Parameters

name − the name of a header field.

Return Value

the value of the named header field, or null if there is no such field in the header.

Exception

NA

Example 1

The following example shows the usage of Java URLConnection getHeaderField(String name) 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(name), we're getting the value of an header field identified by name Content-Type 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();
         String headerValue = urlConnection.getHeaderField("Content-Type");
         System.out.println("Content-Type: " + headerValue);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

Content-Type: text/html; charset=UTF-8

Example 2

The following example shows the usage of Java URLConnection getHeaderField(String name) 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(name), we're getting the value of an header field identified by name Content-Type 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();
         String headerValue = urlConnection.getHeaderField("Content-Type");
         System.out.println("Content-Type: " + headerValue);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

Content-Type: text/html; charset=UTF-8

Example 3

The following example shows the usage of Java URLConnection getHeaderField(String name) 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(name), we're getting the value of an header field identified by name Content-Type 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();
         String headerValue = urlConnection.getHeaderField("Content-Type");
         System.out.println("Content-Type: " + headerValue);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

Content-Type: text/html; charset=ISO-8859-1
java_urlconnection.htm
Advertisements