Java调用Http接口(3)--Commons-HttpClient调用Http接口
程序员文章站
2023-12-29 14:59:22
Commons-HttpClient原来是Apache Commons项目下的一个组件,现已被HttpComponents项目所取代;作为调用Http接口的一种选择,本文介绍下其使用方法。文中所使用到的软件版本:Java 1.8.0_191、Commons-HttpClient 3.1。 1、服务端 ......
commons-httpclient原来是apache commons项目下的一个组件,现已被httpcomponents项目所取代;作为调用http接口的一种选择,本文介绍下其使用方法。文中所使用到的软件版本:java 1.8.0_191、commons-httpclient 3.1。
1、服务端
2、调用
2.1、get请求
public static void get() { try { string requestpath = "http://localhost:8080/webframe/demo/test/getuser?userid=1000&username=" + urlencoder.encode("李白", "utf-8"); httpclient httpclient = new httpclient(); getmethod get = new getmethod(requestpath); int status = httpclient.executemethod(get); if (status == httpstatus.sc_ok) { system.out.println("get返回结果:" + get.getresponsebodyasstring()); } else { system.out.println("get返回状态码:" + status); } } catch (exception e) { e.printstacktrace(); } }
2.2、post请求(发送键值对数据)
public static void post() { try { string requestpath = "http://localhost:8080/webframe/demo/test/getuser"; httpclient httpclient = new httpclient(); postmethod post = new postmethod(requestpath); post.addrequestheader("content-type", "application/x-www-form-urlencoded;charset=utf-8"); string param = "userid=1000&username=李白"; post.setrequestentity(new stringrequestentity(param)); int status = httpclient.executemethod(post); if (status == httpstatus.sc_ok) { system.out.println("post返回结果:" + post.getresponsebodyasstring()); } else { system.out.println("post返回状态码:" + status); } } catch (exception e) { e.printstacktrace(); } }
2.3、post请求(发送json数据)
public static void post2() { try { string requestpath = "http://localhost:8080/webframe/demo/test/adduser"; httpclient httpclient = new httpclient(); postmethod post = new postmethod(requestpath); post.addrequestheader("content-type", "application/json"); string param = "{\"userid\": \"1001\",\"username\":\"杜甫\"}"; post.setrequestentity(new stringrequestentity(param)); int status = httpclient.executemethod(post); if (status == httpstatus.sc_ok) { system.out.println("post返回结果:" + post.getresponsebodyasstring()); } else { system.out.println("post返回状态码:" + status); } } catch (exception e) { e.printstacktrace(); } }
2.4、上传文件及发送键值对数据
public static void multi() { try { string requestpath = "http://localhost:8080/webframe/demo/test/multi"; httpclient httpclient = new httpclient(); postmethod post = new postmethod(requestpath); file file = new file("d:/a.jpg"); part[] parts = {new filepart("file", file), new stringpart("param1", "参数1", "utf-8"), new stringpart("param2", "参数2", "utf-8")}; post.setrequestentity(new multipartrequestentity(parts, post.getparams())); int status = httpclient.executemethod(post); if (status == httpstatus.sc_ok) { system.out.println("multi返回结果:" + post.getresponsebodyasstring()); } else { system.out.println("multi返回状态码:" + status); } } catch (exception e) { e.printstacktrace(); } }
2.5、完整例子
package com.inspur.http; import java.io.file; import java.net.urlencoder; import org.apache.commons.httpclient.httpclient; import org.apache.commons.httpclient.httpstatus; import org.apache.commons.httpclient.methods.getmethod; import org.apache.commons.httpclient.methods.postmethod; import org.apache.commons.httpclient.methods.stringrequestentity; import org.apache.commons.httpclient.methods.multipart.filepart; import org.apache.commons.httpclient.methods.multipart.multipartrequestentity; import org.apache.commons.httpclient.methods.multipart.part; import org.apache.commons.httpclient.methods.multipart.stringpart; /** * * 通过commons-httpclient调用http接口 * */ public class commonshttpclientcase { /** * get请求 */ public static void get() { try { string requestpath = "http://localhost:8080/webframe/demo/test/getuser?userid=1000&username=" + urlencoder.encode("李白", "utf-8"); httpclient httpclient = new httpclient(); getmethod get = new getmethod(requestpath); int status = httpclient.executemethod(get); if (status == httpstatus.sc_ok) { system.out.println("get返回结果:" + get.getresponsebodyasstring()); } else { system.out.println("get返回状态码:" + status); } } catch (exception e) { e.printstacktrace(); } } /** * post请求(发送键值对数据) */ public static void post() { try { string requestpath = "http://localhost:8080/webframe/demo/test/getuser"; httpclient httpclient = new httpclient(); postmethod post = new postmethod(requestpath); post.addrequestheader("content-type", "application/x-www-form-urlencoded;charset=utf-8"); string param = "userid=1000&username=李白"; post.setrequestentity(new stringrequestentity(param)); int status = httpclient.executemethod(post); if (status == httpstatus.sc_ok) { system.out.println("post返回结果:" + post.getresponsebodyasstring()); } else { system.out.println("post返回状态码:" + status); } } catch (exception e) { e.printstacktrace(); } } /** * post请求(发送json数据) */ public static void post2() { try { string requestpath = "http://localhost:8080/webframe/demo/test/adduser"; httpclient httpclient = new httpclient(); postmethod post = new postmethod(requestpath); post.addrequestheader("content-type", "application/json"); string param = "{\"userid\": \"1001\",\"username\":\"杜甫\"}"; post.setrequestentity(new stringrequestentity(param)); int status = httpclient.executemethod(post); if (status == httpstatus.sc_ok) { system.out.println("post返回结果:" + post.getresponsebodyasstring()); } else { system.out.println("post返回状态码:" + status); } } catch (exception e) { e.printstacktrace(); } } /** * 上传文件及发送键值对数据 */ public static void multi() { try { string requestpath = "http://localhost:8080/webframe/demo/test/multi"; httpclient httpclient = new httpclient(); postmethod post = new postmethod(requestpath); file file = new file("d:/a.jpg"); part[] parts = {new filepart("file", file), new stringpart("param1", "参数1", "utf-8"), new stringpart("param2", "参数2", "utf-8")}; post.setrequestentity(new multipartrequestentity(parts, post.getparams())); int status = httpclient.executemethod(post); if (status == httpstatus.sc_ok) { system.out.println("multi返回结果:" + post.getresponsebodyasstring()); } else { system.out.println("multi返回状态码:" + status); } } catch (exception e) { e.printstacktrace(); } } public static void main(string[] args) { get(); post(); post2(); multi(); } }