Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7157360

HttpURLConnection: HTTP method DELETE doesn't support output

XMLWordPrintable

    • b98
    • generic
    • generic
    • Verified

      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)

            arieber Andreas Rieber
            vbaranov Vasiliy Baranov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: