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



Description

The Java URLConnection getRequestProperties() method returns an unmodifiable Map of general request properties for this connection. The Map keys are Strings that represent the request-header field names. Each Map value is a unmodifiable List of Strings that represents the corresponding field values.

Declaration

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

public Map<String,List<String>> getRequestProperties()

Parameters

NA

Return Value

a Map of the general request properties for this connection.

Exception

IllegalStateException − if already connected.

Example 1

The following example shows the usage of Java URLConnection getRequestProperties() 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() method 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());

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

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

Output

{content-type=[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() method 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());

      } 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]}

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() method 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());

      } 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]}

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