HttpURLConnection如何发送PUT,DELETE请求呢?
下文笔者讲述使用HttpURLConnection发送PUT,DELETE请求的方法分享,如下所示:
发送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();
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。