2013-10-11 140 views
9

我有Restful WebServices,并且我发送POST和GET HTTP请求,如何在httpURLConection中用JAVA发送PUT和DELTE请求HTTP。如何在HTTP中发送PUT,DELETE HTTP请求中的HttpURLConnection

+0

至于HttpURLConnection的可以使用[DavidWebb](https://github.com/hgoebl/DavidWebb)称为一个微小的库天然使用的替代方法。在那里你可以找到类似库的列表。本地使用HttpURLConnection非常麻烦并且容易出错。 – hgoebl

回答

23

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

谢谢你的完美!只有我需要方法删除。 – user2816207

+0

不需要调用htpURLConnection.connect()?这个功能是什么? –

-1

可以覆盖

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

执行的HTTP PUT操作;默认实现报告 HTTP BAD_REQUEST错误。 PUT操作类似于通过FTP发送 文件。覆盖此方法的Servlet编写者必须尊重 与请求一起发送的任何Content- *标头。 (这些标头包括内容长度,内容类型,内容传输编码,内容编码,内容基础,内容语言,内容位置,内容MD5和内容范围。)如果子类无法兑现 内容标题,那么它必须发出错误响应(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的副本保存在临时存储中。

+0

这些都是Servlet API方法,但OP询问HTTP客户端我认为 –

+0

也许,但我明白他已经实现了一个管理doGet和doPut的servlet,并且想知道如何执行其他两个操作 – RamonBoza

+0

我有getter和postT,我需要实现删除,并把方法,但是不要来,因为tenngo一个输入参数可以帮助我? - @MatthewGilliard – user2816207

4

如果我使用@BartekM删除请求,它得到此异常:

java.net.ProtocolException: DELETE does not support writing 

要刚修好它,我删除该指令:

// httpURLConnection.setDoOutput(true); 

源:Sending HTTP DELETE request in Android