Java - URLConnection getRequestProperty(String key)
Description
The Java URLConnection getRequestProperty(String key) method returns the value of the named general request property for this connection.
Declaration
Following is the declaration for java.net.URLConnection.getRequestProperty(String key) method
public String getRequestProperty(String key)
Parameters
key − the keyword by which the request is known.
Return Value
the value of the named general request property for this connection. If key is null, then null is returned.
Exception
IllegalStateException − if already connected.
Example 1
The following example shows the usage of Java URLConnection getRequestProperty() 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 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.addRequestProperty("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 getRequestProperty() 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 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.addRequestProperty("content-type", "txt");
urlConnection.addRequestProperty("auth", "basic");
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 3
The following example shows the usage of Java URLConnection getRequestProperty() 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 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.addRequestProperty("content-type", "txt");
urlConnection.addRequestProperty("auth", "basic");
urlConnection.addRequestProperty("content-type", "json");
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
json
Here, we can see the request property is added to existing key and getRequestProperty() return the latest value added to the key.