原生的JAVA API,提供HttpURLConnection请求Http。但是,当需要DELETE操作的时候,HttpURLConnection不允许Output数据。
于此,建议可以使用jersey + common-httpclient来解决这个问题。
需要用到的依赖包:
commons-codec-1.2.jar
commons-httpclient-3.1.jar
javax.ws.rs-api-2.0-m02.jar
jersey-apache-client-1.7.jar
jersey-bundle-1.5.jar
示例:
ApacheHttpClient client = ApacheHttpClient.create();
URI uri = new URI("http://localhost:8080/index/test");
WebResource r = client.resource(uri);
ClientResponse response =
r.accept(MediaType.APPLICATION_XML_TYPE,MediaType.TEXT_PLAIN_TYPE)
.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).delete(ClientResponse.class, "m=ABC");
System.out.println(response.getEntity(String.class));
client.destroy();
更多jersey-api 使用:
http://blog.sina.com.cn/s/blog_62e744e60100rbyf.html
http://stackoverflow.com/questions/16284743/how-to-submit-data-with-jersey-client-post-method
http://hugh-wangp.iteye.com/blog/1797237