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

时间:2013-10-11 14:22:26

标签: java http rest httpurlconnection

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

3 个答案:

答案 0 :(得分:24)

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

答案 1 :(得分:4)

如果我使用@BartekM的DELETE请求,则会出现此异常:

 java.net.ProtocolException: DELETE does not support writing

要解决此问题,我只需删除此说明:

 // httpURLConnection.setDoOutput(true);

来源:Sending HTTP DELETE request in Android

答案 2 :(得分:-1)

你可以覆盖

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

执行HTTP PUT操作;默认实现报告   HTTP BAD_REQUEST错误。 PUT操作类似于发送a   通过FTP文件。重写此方法的Servlet编写者必须尊重   随请求一起发送的任何Content- *标头。 (这些标题包括   内容长度,内容类型,内容传输编码,   内容编码,内容基础,内容语言,内容位置,   content-MD5和content-range。)如果子类不能尊重a   内容标题,然后它必须发出错误响应(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的副本。