Java - HttpURLConnection getRequestMethod() Method
The Java HttpURLConnection getRequestMethod() method returns the request method.
Declaration
Following is the declaration for java.net.HttpURLConnection.getRequestMethod() method
public String getRequestMethod()
Parameters
NA
Return Value
the HTTP request method
Exception
NA
Example 1
The following example shows the usage of Java HttpURLConnection getRequestMethod() 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 HttpURLConnection instance. Using getRequestMethod(), we're checking the request method value printing the same. Now using setRequestMethod() method, we're changing the value of request method and printing the same again −
package com.tutorialspoint;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUrlConnectionDemo {
public static void main(String [] args) {
try {
URL url = new URL("https://www.tutorialspoint.com/index.htm?language=en#j2se");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
String result = urlConnection.getRequestMethod();
System.out.println("request method: " + result);
urlConnection.setRequestMethod("POST");
result = urlConnection.getRequestMethod();
System.out.println("request method: " + result);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
request method: GET request method: POST
Example 2
The following example shows the usage of Java HttpURLConnection getRequestMethod() 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 HttpURLConnection instance. Using getRequestMethod(), we're checking the request method value printing the same. Now using setRequestMethod() method, we're changing the value of request method and printing the same. As connect is established already, setRequestMethod() is throwing an exception as shown below −
package com.tutorialspoint;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUrlConnectionDemo {
public static void main(String [] args) {
try {
URL url = new URL("http://www.tutorialspoint.com/index.htm?language=en#j2se");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
System.out.println("Connected.");
String result = urlConnection.getRequestMethod();
System.out.println("request method: " + result);
urlConnection.setRequestMethod("POST");
result = urlConnection.getRequestMethod();
System.out.println("request method: " + result);
urlConnection.disconnect();
System.out.println("Disconnected.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
Connected. request method: GET Exception in thread "main" java.lang.IllegalStateException: connect in progress at sun.net.www.protocol.http.HttpURLConnection.setRequestMethod(Unknown Source) at com.tutorialspoint.HttpUrlConnectionDemo.main(HttpUrlConnectionDemo.java:18)
Example 3
The following example shows the usage of Java HttpURLConnection getRequestMethod() 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 HttpURLConnection instance. Using getRequestMethod(), we're checking the request method value printing the same. Now using setRequestMethod() method, we're changing the value of request method and printing the same again −
package com.tutorialspoint;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUrlConnectionDemo {
public static void main(String [] args) {
try {
URL url = new URL("http://www.tutorialspoint.com/index.htm?language=en#j2se");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
String result = urlConnection.getRequestMethod();
System.out.println("request method: " + result);
urlConnection.setRequestMethod("POST");
result = urlConnection.getRequestMethod();
System.out.println("request method: " + result);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
request method: GET request method: POST