Fix java.net.ProtocolException: Invalid HTTP method


java.net.ProtocolException: Invalid HTTP method: SET
 at java.base/java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:487)
 at java.base/sun.net.www.protocol.http.HttpURLConnection.setRequestMethod(HttpURLConnection.java:570)
 at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestMethod(HttpsURLConnectionImpl.java:370)
 at HTTPExample.main(HTTPExample.java:21)

ProtocolException invalid HTTP method occurs when you are trying to use an HTTP method that is invalid. Below is the list of valid HTTP methods that you can make use of depending upon what you are trying to achieve (usually associated with CURD at the server end),

  1. POST: Used to perform Create operation.
  2. GET: Used to peform Select operation.
  3. PUT: Used to peform Update/Replace operation.
  4. PATCH: Used to perform Update/Modify operation.
  5. DELETE: Used to perform Delete operation,
Example:
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("SET");
Java ProtocolException Invalid HTTP method
Java ProtocolException Invalid HTTP method

As you can I have used SET as the HTTP Request Method which is an invalid one!



Have Questions? Post them here!

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.

Follow Us: GitHub | Facebook | Twitter | Instagram