java中如何使用HttpURLConnection中发送PUT,DELETE HTTP请求?

java-教程王 Java经验 发布时间:2022-02-09 10:28:05 阅读数:16312 1
下文讲述使用HttpURLConnection发送PUT,DELETE http请求的实现方法分享,如下所示:
实现思路:
    使用HttpURLConnection中的setRequestMethod方法
	放入相应的请求类型,即可实现不同的请求模式
例:
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 delete:
 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/202202/16443744432291.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

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

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者