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