I want to know if it is possible to send PUT, DELETE request (practically) through java.net.HttpURLConnection
to HTTP-based URL.
I have read so many articles describing that how to send GET, POST, TRACE, OPTIONS requests but I still haven't found any sample code which successfully performs PUT and DELETE requests.
Even Rest Template can be an option :
For doing a PUT in HTML correctly, you will have to surround it with try/catch:
To perform an HTTP PUT:
To perform an HTTP DELETE:
This is how it worked for me:
UrlConnection is an awkward API to work with. HttpClient is by far the better API and it'll spare you from loosing time searching how to achieve certain things like this stackoverflow question illustrates perfectly. I write this after having used the jdk HttpUrlConnection in several REST clients. Furthermore when it comes to scalability features (like threadpools, connection pools etc.) HttpClient is superior
I agree with @adietisheim and the rest of people that suggest HttpClient.
I spent time trying to make a simple call to rest service with HttpURLConnection and it hadn't convinced me and after that I tried with HttpClient and it was really more easy, understandable and nice.
An example of code to make a put http call is as follows: