HttpURLConnection如何发送PUT,DELETE请求呢?

java-经验王 Java经验 发布时间:2021-11-04 17:05:13 阅读数:15856 1
下文笔者讲述使用HttpURLConnection发送PUT,DELETE请求的方法分享,如下所示:
实现思路: 
    只需在其setRequestMethod()方法中传入相应的方法类型即可
例:
发送PUT,DELETE请求的示例分享
-----要执行HTTP PUT
URL url = new URL("http://www.java265.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删除
URL url = new URL("http://www.java265.com/resource");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestProperty(
    "Content-Type", "application/x-www-form-urlencoded" );
httpCon.setRequestMethod("DELETE");
httpCon.connect();
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202111/16360167691689.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者