I'm just a starter in java and would like to know how to make a HTTP Delete call to a URL. Any small piece of code or reference material would be very helpful.
I know that the question would sound very simply, but I am in urgent of this information.
I'm just a starter in java and would like to know how to make a HTTP Delete call to a URL. Any small piece of code or reference material would be very helpful.
I know that the question would sound very simply, but I am in urgent of this information.
Actually sending HttpDelete is similar to HttpGet, first you will build the url with all parameters and then just execute the request, the following code is tested.
StringBuilder urlBuilder = new StringBuilder(Config.SERVER)
.append("/api/deleteInfo?").append("id=")
.append(id);
urlBuilder.append("&people=").append(people).toString();
try {
HttpClient httpClient = new DefaultHttpClient();
HttpDelete httpDelete = new HttpDelete(urlBuilder.toString());
HttpResponse httpResponse = httpClient.execute(httpDelete);
HttpEntity entity = httpResponse.getEntity();
final String response = EntityUtils.toString(entity);
Log.d(TAG, "content = " + response);
} catch (final Exception e) {
e.printStackTrace();
Log.d(TAG, "content = " + e.getMessage());
} catch (OutOfMemoryError e) {
e.printStackTrace();
System.gc();
}
DELETE, PUT, OPTIONS methods are restricted by most of the servers. Here is good discussion about this topic in SO. Are the PUT, DELETE, HEAD, etc methods available in most web browsers?
You can use Restlet. Its a good client API.Or you can do as following
URL url = new URL("http://www.example.com/resource");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestProperty(
"Content-Type", "application/x-www-form-urlencoded" );
httpCon.setRequestMethod("DELETE");
httpCon.connect();
httpCon.getInputStream()
I guess you can call like this :
URL url = new URL("http://www.abcd.com/blog");
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
httpConnection.setDoOutput(true);
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded" );
httpConnection.setRequestMethod("DELETE");
httpConnection.connect();
You can also try the Apache HttpClient, it provides an API for all HTTP methods (GET, PUT, DELETE, POST, OPTIONS, HEAD, and TRACE).
For a sample look here: http://hc.apache.org/httpclient-3.x/methods/delete. API reference is here: http://hc.apache.org/httpclient-3.x/apidocs/index.html
Cheers