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!



Recent Posts:




Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.