2

I have created web service call using java below code. Now I need to make delete and put operations to be perform.

URL url = new URL("http://example.com/questions");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod( "POST" );
conn.setRequestProperty("Content-Type", "application/json");

OutputStream os = conn.getOutputStream();
os.write(jsonBody.getBytes());
os.flush();

When I add below code to perform DELETE action it gives errors saying:

java.net.ProtocolException: HTTP method DELETE doesn't support output.

conn.setRequestMethod( "DELETE" );

So how to perform delete and put requests?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
manitaz
  • 1,181
  • 2
  • 9
  • 26
  • Please excuse the shouting, but **WHAT** errors does it give? Also, I would suggest [`HttpClient`](http://hc.apache.org/httpcomponents-client-ga/index.html) from apache. – Elliott Frisch Nov 14 '14 at 05:43
  • edited the question. Any sample codes for web service requests? Any help? – manitaz Nov 14 '14 at 05:46
  • Does http://stackoverflow.com/questions/1051004/how-to-send-put-delete-http-request-in-httpurlconnection help? – hgoebl Nov 14 '14 at 06:36
  • I will checked this but it does not worked for me some reason. – manitaz Nov 14 '14 at 06:43

3 Answers3

7

PUT example using HttpURLConnection:

URL url = null;
try {
   url = new URL("http://localhost:8080/putservice");
} catch (MalformedURLException exception) {
    exception.printStackTrace();
}
HttpURLConnection httpURLConnection = null;
DataOutputStream dataOutputStream = null;
try {
    httpURLConnection = (HttpURLConnection) url.openConnection();
    httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    httpURLConnection.setRequestMethod("PUT");
    httpURLConnection.setDoInput(true);
    httpURLConnection.setDoOutput(true);
    dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
    dataOutputStream.write("hello");
} catch (IOException exception) {
    exception.printStackTrace();
}  finally {
    if (dataOutputStream != null) {
        try {
            dataOutputStream.flush();
            dataOutputStream.close();
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }
    if (httpsURLConnection != null) {
        httpsURLConnection.disconnect();
    }
}

DELETE example using HttpURLConnection:

URL url = null;
try {
    url = new URL("http://localhost:8080/deleteservice");
} catch (MalformedURLException exception) {
    exception.printStackTrace();
}
HttpURLConnection httpURLConnection = null;
try {
    httpURLConnection = (HttpURLConnection) url.openConnection();
    httpURLConnection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
    httpURLConnection.setRequestMethod("DELETE");
    System.out.println(httpURLConnection.getResponseCode());
} catch (IOException exception) {
    exception.printStackTrace();
} finally {         
    if (httpURLConnection != null) {
        httpURLConnection.disconnect();
    }
}
Nordehinu
  • 338
  • 1
  • 3
  • 11
Jamsheer
  • 3,673
  • 3
  • 29
  • 57
3

FOR PUT

URL url = new URL("http://www.example.com/resource");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
OutputStreamWriter out = new OutputStreamWriter(
    httpCon.getOutputStream());
out.write("Resource content");
out.close();
httpCon.getInputStream();

FOR DELETE

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();

Actually got it from the link here: Send PUT, DELETE HTTP request in HttpURLConnection

Community
  • 1
  • 1
Young Emil
  • 2,220
  • 2
  • 26
  • 37
2

i suggest you to use restlet client for web service request .please refer the bellow sample code ,it may help you

 Client client = new Client(new Context(), Protocol.HTTP);
   clientResource = new ClientResource(url);
        ResponseRepresentation responseRep = null;
         try {
            clientResource.setNext(client);           
            clientResource.delete();

       } catch (Exception e) {
           e.printStackTrace();

       }
Jamsheer
  • 3,673
  • 3
  • 29
  • 57