Java调用Http接口(4)--HttpClient调用Http接口
程序员文章站
2022-07-02 12:22:38
HttpClient是Apache HttpComponents项目下的一个组件,是Commons-HttpClient的升级版,两者api调用写法也很类似。文中所使用到的软件版本:Java 1.8.0_191、HttpClient 4.5.10。 1、服务端 参见Java调用Http接口(1)-- ......
httpclient是apache httpcomponents项目下的一个组件,是commons-httpclient的升级版,两者api调用写法也很类似。文中所使用到的软件版本:java 1.8.0_191、httpclient 4.5.10。
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"); closeablehttpclient httpclient = httpclients.createdefault(); httpget get = new httpget(requestpath); closeablehttpresponse response = httpclient.execute(get); system.out.println("get返回状态:" + response.getstatusline()); httpentity responseentity = response.getentity(); system.out.println("get返回结果:" + entityutils.tostring(responseentity)); //流畅api调用 string result = request.get(requestpath).execute().returncontent().tostring(); system.out.println("get fluent返回结果:" + result); } catch (exception e) { e.printstacktrace(); } }
2.2、post请求(发送键值对数据)
public static void post() { try { string requestpath = "http://localhost:8080/webframe/demo/test/getuser"; closeablehttpclient httpclient = httpclients.createdefault(); httppost post = new httppost(requestpath); list<namevaluepair> list = new arraylist<namevaluepair>(); list.add(new basicnamevaluepair("userid", "1000")); list.add(new basicnamevaluepair("username", "李白")); post.setentity(new urlencodedformentity(list, "utf-8")); closeablehttpresponse response = httpclient.execute(post); system.out.println("post返回状态:" + response.getstatusline()); httpentity responseentity = response.getentity(); system.out.println("post返回结果:" + entityutils.tostring(responseentity)); //流畅api调用 string result = request.post(requestpath) .bodyform(form.form().add("userid", "1000").add("username", "李白").build(), charset.forname("utf-8")) .execute().returncontent().tostring(); system.out.println("post fluent返回结果:" + result); } catch (exception e) { e.printstacktrace(); } }
2.3、post请求(发送json数据)
public static void post2() { try { string requestpath = "http://localhost:8080/webframe/demo/test/adduser"; closeablehttpclient httpclient = httpclients.createdefault(); httppost post = new httppost(requestpath); post.setheader("content-type", "application/json"); string param = "{\"userid\": \"1001\",\"username\":\"杜甫\"}"; post.setentity(new stringentity(param, "utf-8")); closeablehttpresponse response = httpclient.execute(post); system.out.println("post json返回状态:" + response.getstatusline()); httpentity responseentity = response.getentity(); system.out.println("post josn返回结果:" + entityutils.tostring(responseentity)); //流畅api调用 string result = request.post(requestpath) .addheader("content-type", "application/json") .bodystring(param, contenttype.application_json) .execute().returncontent().tostring(); system.out.println("post json fluent返回结果:" + result); } catch (exception e) { e.printstacktrace(); } }
2.4、上传文件及发送键值对数据
public static void multi() { try { string requestpath = "http://localhost:8080/webframe/demo/test/multi"; closeablehttpclient httpclient = httpclients.createdefault(); httppost post = new httppost(requestpath); filebody file = new filebody(new file("d:/a.jpg")); httpentity requestentity = multipartentitybuilder.create() .addpart("file", file) .addpart("param1", new stringbody("参数1", contenttype.create(contenttype.text_plain.getmimetype(), "utf-8"))) .addpart("param2", new stringbody("参数2", contenttype.create(contenttype.text_plain.getmimetype(), "utf-8"))) .build(); post.setentity(requestentity); closeablehttpresponse response = httpclient.execute(post); system.out.println("multi返回状态:" + response.getstatusline()); httpentity responseentity = response.getentity(); system.out.println("multi返回结果:" + entityutils.tostring(responseentity)); //流畅api调用 string result = request.post(requestpath) .body(multipartentitybuilder.create() .addpart("file", file) .addpart("param1", new stringbody("参数1", contenttype.create(contenttype.text_plain.getmimetype(), "utf-8"))) .addpart("param2", new stringbody("参数2", contenttype.create(contenttype.text_plain.getmimetype(), "utf-8"))) .build()) .execute().returncontent().tostring(); system.out.println("multi fluent返回结果:" + result); } catch (exception e) { e.printstacktrace(); } }
2.5、完整例子
package com.inspur.demo.http; import java.io.file; import java.net.urlencoder; import java.nio.charset.charset; import java.util.arraylist; import java.util.list; import org.apache.http.httpentity; import org.apache.http.namevaluepair; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.fluent.form; import org.apache.http.client.fluent.request; import org.apache.http.client.methods.closeablehttpresponse; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.entity.contenttype; import org.apache.http.entity.stringentity; import org.apache.http.entity.mime.multipartentitybuilder; import org.apache.http.entity.mime.content.filebody; import org.apache.http.entity.mime.content.stringbody; import org.apache.http.impl.client.closeablehttpclient; import org.apache.http.impl.client.httpclients; import org.apache.http.message.basicnamevaluepair; import org.apache.http.util.entityutils; /** * 通过commons-httpclient调用http接口 * */ public class httpclientcase { /** * get请求 */ public static void get() { try { string requestpath = "http://localhost:8080/webframe/demo/test/getuser?userid=1000&username=" + urlencoder.encode("李白", "utf-8"); closeablehttpclient httpclient = httpclients.createdefault(); httpget get = new httpget(requestpath); closeablehttpresponse response = httpclient.execute(get); system.out.println("get返回状态:" + response.getstatusline()); httpentity responseentity = response.getentity(); system.out.println("get返回结果:" + entityutils.tostring(responseentity)); //流畅api调用 string result = request.get(requestpath).execute().returncontent().tostring(); system.out.println("get fluent返回结果:" + result); } catch (exception e) { e.printstacktrace(); } } /** * post请求(发送键值对数据) */ public static void post() { try { string requestpath = "http://localhost:8080/webframe/demo/test/getuser"; closeablehttpclient httpclient = httpclients.createdefault(); httppost post = new httppost(requestpath); list<namevaluepair> list = new arraylist<namevaluepair>(); list.add(new basicnamevaluepair("userid", "1000")); list.add(new basicnamevaluepair("username", "李白")); post.setentity(new urlencodedformentity(list, "utf-8")); closeablehttpresponse response = httpclient.execute(post); system.out.println("post返回状态:" + response.getstatusline()); httpentity responseentity = response.getentity(); system.out.println("post返回结果:" + entityutils.tostring(responseentity)); //流畅api调用 string result = request.post(requestpath) .bodyform(form.form().add("userid", "1000").add("username", "李白").build(), charset.forname("utf-8")) .execute().returncontent().tostring(); system.out.println("post fluent返回结果:" + result); } catch (exception e) { e.printstacktrace(); } } /** * post请求(发送json数据) */ public static void post2() { try { string requestpath = "http://localhost:8080/webframe/demo/test/adduser"; closeablehttpclient httpclient = httpclients.createdefault(); httppost post = new httppost(requestpath); post.setheader("content-type", "application/json"); string param = "{\"userid\": \"1001\",\"username\":\"杜甫\"}"; post.setentity(new stringentity(param, "utf-8")); closeablehttpresponse response = httpclient.execute(post); system.out.println("post json返回状态:" + response.getstatusline()); httpentity responseentity = response.getentity(); system.out.println("post josn返回结果:" + entityutils.tostring(responseentity)); //流畅api调用 string result = request.post(requestpath) .addheader("content-type", "application/json") .bodystring(param, contenttype.application_json) .execute().returncontent().tostring(); system.out.println("post json fluent返回结果:" + result); } catch (exception e) { e.printstacktrace(); } } /** * 上传文件及发送键值对数据 */ public static void multi() { try { string requestpath = "http://localhost:8080/webframe/demo/test/multi"; closeablehttpclient httpclient = httpclients.createdefault(); httppost post = new httppost(requestpath); filebody file = new filebody(new file("d:/a.jpg")); httpentity requestentity = multipartentitybuilder.create() .addpart("file", file) .addpart("param1", new stringbody("参数1", contenttype.create(contenttype.text_plain.getmimetype(), "utf-8"))) .addpart("param2", new stringbody("参数2", contenttype.create(contenttype.text_plain.getmimetype(), "utf-8"))) .build(); post.setentity(requestentity); closeablehttpresponse response = httpclient.execute(post); system.out.println("multi返回状态:" + response.getstatusline()); httpentity responseentity = response.getentity(); system.out.println("multi返回结果:" + entityutils.tostring(responseentity)); //流畅api调用 string result = request.post(requestpath) .body(multipartentitybuilder.create() .addpart("file", file) .addpart("param1", new stringbody("参数1", contenttype.create(contenttype.text_plain.getmimetype(), "utf-8"))) .addpart("param2", new stringbody("参数2", contenttype.create(contenttype.text_plain.getmimetype(), "utf-8"))) .build()) .execute().returncontent().tostring(); system.out.println("multi fluent返回结果:" + result); } catch (exception e) { e.printstacktrace(); } } public static void main(string[] args) { get(); post(); post2(); multi(); } }
推荐阅读