如何在JAVA中使用HttpURLConnection发送PUT和DELETE请求

10

我有一个Restful WebServices,我发送POST和GET HTTP请求,如何使用JAVA中的httpURLConection发送PUT和DELETE请求HTTP。


作为 HttpURLConnection 本地使用的替代方案,您可以使用一个名为 DavidWebb 的小型库。在那里,您可以找到一系列类似的库。本地使用 HttpURLConnection 很麻烦且容易出错。 - hgoebl
3个回答

25

放置

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

删除

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

我不需要调用htpURLConnection.connect()吗?这个函数是干什么用的? - Bibaswann Bandyopadhyay

5
如果我使用@BartekM的DELETE请求,会出现以下异常:
 java.net.ProtocolException: DELETE does not support writing

为了解决这个问题,我只需要删除这条指令:
 // httpURLConnection.setDoOutput(true);

source: Sending HTTP DELETE request in Android


-1
你可以重写
protected void doPut(HttpServletRequest req,
                      HttpServletResponse resp) throws ServletException, IOException;

执行HTTP PUT操作;默认实现报告HTTP BAD_REQUEST错误。PUT操作类似于通过FTP发送文件。覆盖此方法的Servlet编写者必须尊重请求中发送的任何Content-*标头。 (这些标头包括content-length、content-type、content-transfer-encoding、content-encoding、content-base、content-language、content-location、content-MD5和content-range。)如果子类无法遵守内容标头,则必须发出错误响应(501)并丢弃请求。有关更多信息,请参见HTTP 1.1 RFC。

此方法不需要是“安全”或“幂等”的。通过PUT请求的操作可能具有用户可以承担责任的副作用。虽然不是必需的,但覆盖此方法的Servlet编写者可能希望将受影响的URI的副本保存在临时存储中。

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

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

这些是Servlet API方法,但我认为OP正在询问一个HTTP客户端。 - Matthew Gilliard
也许是这样,但我理解他已经实现了一个管理doGet和doPut的servlet,并想知道如何实现另外两个操作。 - RamonBoza
如果我已经有了getter和postT,我需要实现delete和put方法,但是我不知道该怎么做,因为我只有一个输入参数可以使用?- @MatthewGilliard - user2816207

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接