我有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