如何在 JAVA 中的 HttpURLConnection 中发送 PUT、DELETE HTTP 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19320555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:04:41  来源:igfitidea点击:

How to send PUT, DELETE HTTP request in HttpURLConnection in JAVA

javahttpresthttpurlconnection

提问by user2816207

I have Restful WebServices, and i send POST and GET HTTP request, how to send PUT and DELTE request HTTP in httpURLConection with JAVA.

我有 Restful WebServices,我发送 POST 和 GET HTTP 请求,如何使用 JAVA 在 httpURLConection 中发送 PUT 和 DELTE 请求 HTTP。

采纳答案by bmlynarczyk

PUT

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

删除

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

回答by RamonBoza

you can override

你可以覆盖

protected void doPut(HttpServletRequest req,
                      HttpServletResponse resp) throws ServletException, IOException;

Performs the HTTP PUT operation; the default implementation reports an HTTP BAD_REQUEST error. The PUT operation is analogous to sending a file via FTP. Servlet writers who override this method must respect any Content-* headers sent with the request. (These headers include content-length, content-type, content-transfer-encoding, content-encoding, content-base, content-language, content-location, content-MD5, and content-range.) If the subclass cannot honor a content header, then it must issue an error response (501) and discard the request. For more information, see the HTTP 1.1 RFC.

This method does not need to be either "safe" or "idempotent". Operations requested through PUT can have side effects for which the user can be held accountable. Although not required, servlet writers who override this method may wish to save a copy of the affected URI in temporary storage.

执行 HTTP PUT 操作;默认实现报告 HTTP BAD_REQUEST 错误。PUT 操作类似于通过 FTP 发送文件。覆盖此方法的 Servlet 编写者必须尊重随请求发送的任何 Content-* 标头。(这些标头包括内容长度、内容类型、内容传输编码、内容编码、内容基础、内容语言、内容位置、内容 MD5 和内容范围。)如果子类不能接受内容头,那么它必须发出错误响应(501)并丢弃请求。有关更多信息,请参阅 HTTP 1.1 RFC。

这种方法不需要“安全”或“幂等”。通过 PUT 请求的操作可能会产生副作用,用户可以对此负责。尽管不是必需的,但覆盖此方法的 servlet 编写者可能希望在临时存储中保存受影响 URI 的副本。

and

 protected void doDelete(HttpServletRequest req,
                         HttpServletResponse resp) throws ServletException, IOException

Performs the HTTP DELETE operation; the default implementation reports an HTTP BAD_REQUEST error. The DELETE operation allows a client to request a URI to be removed from the server. This method does not need to be either "safe" or "idempotent". Operations requested through DELETE can have side-effects for which users may be held accountable. Although not required, servlet writers who subclass this method may wish to save a copy of the affected URI in temporary storage.

执行 HTTP DELETE 操作;默认实现报告 HTTP BAD_REQUEST 错误。DELETE 操作允许客户端请求从服务器删除 URI。这种方法不需要“安全”或“幂等”。通过 DELETE 请求的操作可能会产生副作用,用户可能需要对此负责。尽管不是必需的,但是继承此方法的 servlet 编写者可能希望在临时存储中保存受影响 URI 的副本。

回答by spritus

If I use the DELETE request from @BartekM, it get this exception:

如果我使用来自@BartekM 的 DELETE 请求,它会得到这个异常:

 java.net.ProtocolException: DELETE does not support writing

To fix it, I just remove this instruction:

为了修复它,我只是删除了这个指令:

 // httpURLConnection.setDoOutput(true);

source: Sending HTTP DELETE request in Android

来源:在 Android 中发送 HTTP DELETE 请求