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

查看:1637
本文介绍了如何在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天全站免登陆