JDK-7157360 : HttpURLConnection: HTTP method DELETE doesn't support output
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 6u31
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2012-03-28
  • Updated: 2015-09-02
  • Resolved: 2013-06-22
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8 b98Fixed
Related Reports
Relates :  
Description
When using HttpURLConnection, if I set the request method to "DELETE" and attempt to get to the output stream to write the entity body, I get:

    Exception in thread "main" java.net.ProtocolException: HTTP method DELETE doesn't support output
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1004)

As it turns out, sun.net.www.protocol.http.HttpURLConnection explicitly denies access to the output stream if the request method is DELETE.

This restriction is likely a bug in the HttpURLConnection implementation, as RFC 2616 does not explicitly forbid or discourage request bodies in DELETE requests.

Note that Apache HTTP Client and all major browsers do allow request bodies in DELETE requests.

This problem is actually the root cause of the following JavaFX issue: http://javafx-jira.kenai.com/browse/RT-20664 (WebView is unable to include request entities in DELETE requests due to HttpURLConnection limitation)

Test program:

public class DeleteWitBodyTest {
    public static void main(String[] args) throws IOException {
        String url = "http://javafx-jira.kenai.com/rest/api/1.0/issues/47181/watchers";
        HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
        c.setRequestMethod("DELETE");
        c.setDoOutput(true);
        OutputStream os = c.getOutputStream();
        os.write("dummy".getBytes("UTF-8"));
        os.close();
    }
}

Expected output:

    <None>

Actual output:

    Exception in thread "main" java.net.ProtocolException: HTTP method DELETE doesn't support output
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1004)
        at deletewitbodytest.DeleteWitBodyTest.main(DeleteWitBodyTest.java:14)

Comments
Verified in b103
23-08-2013