如何在 HttpURLConnection 中发送 PUT、DELETE HTTP 请求? [英] How to send PUT, DELETE HTTP request in HttpURLConnection?

查看:62
本文介绍了如何在 HttpURLConnection 中发送 PUT、DELETE HTTP 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以通过 java.net.HttpURLConnection 向基于 HTTP 的 URL 发送 PUT、DELETE 请求(实际上).

I want to know if it is possible to send PUT, DELETE request (practically) through java.net.HttpURLConnection to HTTP-based URL.

我已经阅读了很多描述如何发送 GET、POST、TRACE、OPTIONS 请求的文章,但我仍然没有找到任何成功执行 PUT 和 DELETE 请求的示例代码.

I have read so many articles describing that how to send GET, POST, TRACE, OPTIONS requests but I still haven't found any sample code which successfully performs PUT and DELETE requests.

推荐答案

执行 HTTP PUT:

To perform an HTTP PUT:

URL url = new URL("http://www.example.com/resource");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
OutputStreamWriter out = new OutputStreamWriter(
    httpCon.getOutputStream());
out.write("Resource content");
out.close();
httpCon.getInputStream();

要执行 HTTP DELETE:

To perform an HTTP DELETE:

URL url = new URL("http://www.example.com/resource");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestProperty(
    "Content-Type", "application/x-www-form-urlencoded" );
httpCon.setRequestMethod("DELETE");
httpCon.connect();

这篇关于如何在 HttpURLConnection 中发送 PUT、DELETE HTTP 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆