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



Description

The Java URLConnection addRequestProperty(String key, String value) method adds a general request property specified by a key-value pair. This method will not overwrite existing values associated with the same key.

Declaration

Following is the declaration for java.net.URLConnection.addRequestProperty(String key, String value) method

public void addRequestProperty(String key, String value)

Parameters

key − the keyword by which the request is known (e.g., "Accept").

value − the value associated with it.

Return Value

NA

Exception

IllegalStateException − if already connected.

NullPointerException − if key is null.

Example 1

The following example shows the usage of Java URLConnection addRequestProperty() 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 addRequestProperty(), we're adding a request property and then checking the same using getRequestProperties() and getRequestProperty() methods 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();

         urlConnection.addRequestProperty("content-type", "txt");
         System.out.println(urlConnection.getRequestProperties());
         System.out.println(urlConnection.getRequestProperty("content-type"));

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

{content-type=[txt]}
txt

Example 2

The following example shows the usage of Java URLConnection addRequestProperty() 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 addRequestProperty(), we're adding multiple request properties and then checking the same using getRequestProperties() and getRequestProperty() methods 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();

         urlConnection.addRequestProperty("content-type", "txt");
         urlConnection.addRequestProperty("auth", "basic");
         System.out.println(urlConnection.getRequestProperties());
         System.out.println(urlConnection.getRequestProperty("content-type"));

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

{auth=[basic], content-type=[txt]}
txt

Example 3

The following example shows the usage of Java URLConnection addRequestProperty() 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 addRequestProperty(), we're adding multiple request properties while repeating a key and then checking the same using getRequestProperties() and getRequestProperty() methods 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();

         urlConnection.addRequestProperty("content-type", "txt");
         urlConnection.addRequestProperty("auth", "basic");
         urlConnection.addRequestProperty("content-type", "json");
         System.out.println(urlConnection.getRequestProperties());
         System.out.println(urlConnection.getRequestProperty("content-type"));

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

{auth=[basic], content-type=[json, txt]}
json

Here, we can see the request property is added to existing key and getRequestProperty() return the latest value added to the key.

java_urlconnection.htm
Advertisements