Java - URLConnection setRequestProperty(String key, String value)
Description
The Java URLConnection setRequestProperty(String key, String value) method sets the general request property. If a property with the key already exists, overwrite its value with the new value.
Declaration
Following is the declaration for java.net.URLConnection.setRequestProperty(String key, String value) method
public void setRequestProperty(String key, String value)
Parameters
key − the keyword by which the request is known.
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 setRequestProperty() 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 setRequestProperty(), we're setting a request property and then checking the same using getRequestProperty() 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.setRequestProperty("content-type", "txt");
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
txt
Example 2
The following example shows the usage of Java URLConnection setRequestProperty() 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 setRequestProperty(), 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.setRequestProperty("content-type", "txt");
urlConnection.setRequestProperty("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 setRequestProperty() 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 setRequestProperty(), 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.setRequestProperty("content-type", "txt");
urlConnection.setRequestProperty("auth", "basic");
urlConnection.setRequestProperty("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]}
Here, we can see the request property is set to existing key and getRequestProperties() return the latest value added to the key.
java_urlconnection.htm