我有Restful WebServices,並且我發送POST和GET HTTP請求,如何在httpURLConection中用JAVA發送PUT和DELTE請求HTTP。如何在HTTP中發送PUT,DELETE HTTP請求中的HttpURLConnection
回答
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();
}
}
謝謝你的完美!只有我需要方法刪除。 – user2816207
不需要調用htpURLConnection.connect()?這個功能是什麼? –
可以覆蓋
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的副本保存在臨時存儲中。
這些都是Servlet API方法,但OP詢問HTTP客戶端我認爲 –
也許,但我明白他已經實現了一個管理doGet和doPut的servlet,並且想知道如何執行其他兩個操作 – RamonBoza
我有getter和postT,我需要實現刪除,並把方法,但是不要來,因爲tenngo一個輸入參數可以幫助我? - @MatthewGilliard – user2816207
如果我使用@BartekM刪除請求,它得到此異常:
java.net.ProtocolException: DELETE does not support writing
要剛修好它,我刪除該指令:
// httpURLConnection.setDoOutput(true);
- 1. 如何在HttpURLConnection中發送PUT,DELETE HTTP請求?
- 2. 在HttpURLConnection中發送PUT,刪除HTTP請求
- 3. 可以使用瀏覽器發送PUT或DELETE HTTP請求嗎?
- 4. 從瀏覽器發送http PUT/GET/DELETE/POST請求
- 5. 如何在WHM插件中創建HTTP請求(POST,GET,DELETE,PUT)?
- 6. 如何使用ionic2 native http插件發送HTTP PUT請求?
- 7. HTTP DELETE請求
- 8. Http Put請求
- 9. 如何在android中發送http請求?
- 10. HTTP PUT請求通常如何發佈?
- 11. 如何在jQuery中發送PUT/DELETE請求?
- 12. AFNetworking HTTP PUT請求
- 13. 使用CURL發送`PUT`和`DELETE`請求
- 14. 發送HTTP請求
- 15. 發送HTTP請求
- 16. 發送的HTTP PUT請求中的一個整數的Java
- 17. 如何發送來自Haxe/Neko的HTTP PUT請求?
- 18. 如何使用curl發送HTTP PUT請求的空URI
- 19. 如何在Python中發送HTTP標頭請求而不是HTTP
- 20. 如何在Pycurl中發送DELETE請求?
- 21. 如何用「DELETE」HTTP動詞發送請求?
- 22. 如何在ZendFramework2中發送PUT請求?
- 23. 如何在http中發送數組請求請求
- 24. 的jqGrid的在線編輯與REST API(POST,PUT,DELETE HTTP請求)
- 25. 我可以在Delphi 7的PUT和DELETE中發送請求嗎?
- 26. Java:使用HttpURLConnection的HTTP PUT
- 27. 在Ruby中發送HTTP/2 POST請求
- 28. 在Qt中發送HTTP請求
- 29. Restlet:在HTTP請求中發送xml
- 30. 在node.js中發送http請求
至於HttpURLConnection的可以使用[DavidWebb](https://github.com/hgoebl/DavidWebb)稱爲一個微小的庫天然使用的替代方法。在那裏你可以找到類似庫的列表。本地使用HttpURLConnection非常麻煩並且容易出錯。 – hgoebl