6

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Winz
  • 237
  • 3
  • 6
  • 14
  • 3
    Do you know how to make an HTTP GET call to a URL? – John Saunders Dec 23 '11 at 19:53
  • No, is it necessary to make a get call before we perform a delete?? – Winz Dec 25 '11 at 16:24
  • I mean that if you know how to do a GET, then you know how to do a DELETE. – John Saunders Dec 25 '11 at 18:30
  • Can you post some code,so that can help find solution for your problem? – kvc Dec 26 '11 at 03:57
  • HttpClient client = new HttpClient(); DeleteMethod del = new DeleteMethod(url); client.setConnectionTimeout(8000); client.executeMethod(del); del.setRequestHeader("Content-Type", "text/xml" ); – Winz Dec 27 '11 at 09:34
  • I had tried the above method, I know how to use the Get method and that works fine without any issues. I am able to retrive the values on the URL using the GET method – Winz Dec 27 '11 at 09:34

5 Answers5

1

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();
    }
vanloi999
  • 485
  • 4
  • 13
  • can you tell me where to put headres? like BasicHeader(HTTP.CONTENT_TYPE, "application/json") – 5er Feb 10 '14 at 07:28
1

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?

Community
  • 1
  • 1
kosa
  • 65,990
  • 13
  • 130
  • 167
  • Additionally, some servers don't accept DELETE requests with a request-body. So it's not just browsers we need to be weary of. +1 – jamesmortensen Jan 19 '12 at 03:18
0

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()
kvc
  • 1,127
  • 2
  • 7
  • 21
  • Hi, the above method doesn't work it returns 'Bad Request'. I have tried even the DeleteMethod delete = new DeleteMethod('http://www.example.com/resource'); it doesn't for this too. – Winz Dec 25 '11 at 15:22
  • Could you please let me know how to execute the DeleteMethod delete = new DeleteMethod('http://example.com/resource'); – Winz Dec 25 '11 at 15:44
  • HttpClient client = new HttpClient(); DeleteMethod del = new DeleteMethod(url); client.setConnectionTimeout(8000); client.executeMethod(del); del.setRequestHeader("Content-Type", "text/xml" ); – Winz Dec 27 '11 at 09:32
  • I had tried the above method, I know how to use the Get method and that works fine without any issues. – Winz Dec 27 '11 at 09:33
0

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();
Sabya
  • 1,419
  • 1
  • 10
  • 14
0

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

Stihler
  • 114
  • 4