使用HttpClient调用接口的实例讲解
程序员文章站
2024-02-26 16:40:34
一,编写返回对象
public class httpresult {
// 响应的状态码
private int code;
// 响应的响应体
pr...
一,编写返回对象
public class httpresult { // 响应的状态码 private int code; // 响应的响应体 private string body; get/set… }
二,封装httpclient
package cn.xxxxxx.httpclient; import java.util.arraylist; import java.util.list; import java.util.map; import org.apache.http.namevaluepair; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.closeablehttpresponse; import org.apache.http.client.methods.httpdelete; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.client.methods.httpput; import org.apache.http.client.utils.uribuilder; 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; public class apiservice { private closeablehttpclient httpclient; public apiservice() { // 1 创建httpclinet,相当于打开浏览器 this.httpclient = httpclients.createdefault(); } /** * 带参数的get请求 * * @param url * @param map * @return * @throws exception */ public httpresult doget(string url, map<string, object> map) throws exception { // 声明uribuilder uribuilder uribuilder = new uribuilder(url); // 判断参数map是否为非空 if (map != null) { // 遍历参数 for (map.entry<string, object> entry : map.entryset()) { // 设置参数 uribuilder.setparameter(entry.getkey(), entry.getvalue().tostring()); } } // 2 创建httpget对象,相当于设置url请求地址 httpget httpget = new httpget(uribuilder.build()); // 3 使用httpclient执行httpget,相当于按回车,发起请求 closeablehttpresponse response = this.httpclient.execute(httpget); // 4 解析结果,封装返回对象httpresult,相当于显示相应的结果 // 状态码 // response.getstatusline().getstatuscode(); // 响应体,字符串,如果response.getentity()为空,下面这个代码会报错,所以解析之前要做非空的判断 // entityutils.tostring(response.getentity(), "utf-8"); httpresult httpresult = null; // 解析数据封装httpresult if (response.getentity() != null) { httpresult = new httpresult(response.getstatusline().getstatuscode(), entityutils.tostring(response.getentity(), "utf-8")); } else { httpresult = new httpresult(response.getstatusline().getstatuscode(), ""); } // 返回 return httpresult; } /** * 不带参数的get请求 * * @param url * @return * @throws exception */ public httpresult doget(string url) throws exception { httpresult httpresult = this.doget(url, null); return httpresult; } /** * 带参数的post请求 * * @param url * @param map * @return * @throws exception */ public httpresult dopost(string url, map<string, object> map) throws exception { // 声明httppost请求 httppost httppost = new httppost(url); // 判断map不为空 if (map != null) { // 声明存放参数的list集合 list<namevaluepair> params = new arraylist<namevaluepair>(); // 遍历map,设置参数到list中 for (map.entry<string, object> entry : map.entryset()) { params.add(new basicnamevaluepair(entry.getkey(), entry.getvalue().tostring())); } // 创建form表单对象 urlencodedformentity formentity = new urlencodedformentity(params, "utf-8"); // 把表单对象设置到httppost中 httppost.setentity(formentity); } // 使用httpclient发起请求,返回response closeablehttpresponse response = this.httpclient.execute(httppost); // 解析response封装返回对象httpresult httpresult httpresult = null; if (response.getentity() != null) { httpresult = new httpresult(response.getstatusline().getstatuscode(), entityutils.tostring(response.getentity(), "utf-8")); } else { httpresult = new httpresult(response.getstatusline().getstatuscode(), ""); } // 返回结果 return httpresult; } /** * 不带参数的post请求 * * @param url * @return * @throws exception */ public httpresult dopost(string url) throws exception { httpresult httpresult = this.dopost(url, null); return httpresult; } /** * 带参数的put请求 * * @param url * @param map * @return * @throws exception */ public httpresult doput(string url, map<string, object> map) throws exception { // 声明httppost请求 httpput httpput = new httpput(url); // 判断map不为空 if (map != null) { // 声明存放参数的list集合 list<namevaluepair> params = new arraylist<namevaluepair>(); // 遍历map,设置参数到list中 for (map.entry<string, object> entry : map.entryset()) { params.add(new basicnamevaluepair(entry.getkey(), entry.getvalue().tostring())); } // 创建form表单对象 urlencodedformentity formentity = new urlencodedformentity(params, "utf-8"); // 把表单对象设置到httppost中 httpput.setentity(formentity); } // 使用httpclient发起请求,返回response closeablehttpresponse response = this.httpclient.execute(httpput); // 解析response封装返回对象httpresult httpresult httpresult = null; if (response.getentity() != null) { httpresult = new httpresult(response.getstatusline().getstatuscode(), entityutils.tostring(response.getentity(), "utf-8")); } else { httpresult = new httpresult(response.getstatusline().getstatuscode(), ""); } // 返回结果 return httpresult; } /** * 带参数的delete请求 * * @param url * @param map * @return * @throws exception */ public httpresult dodelete(string url, map<string, object> map) throws exception { // 声明uribuilder uribuilder uribuilder = new uribuilder(url); // 判断参数map是否为非空 if (map != null) { // 遍历参数 for (map.entry<string, object> entry : map.entryset()) { // 设置参数 uribuilder.setparameter(entry.getkey(), entry.getvalue().tostring()); } } // 2 创建httpget对象,相当于设置url请求地址 httpdelete httpdelete = new httpdelete(uribuilder.build()); // 3 使用httpclient执行httpget,相当于按回车,发起请求 closeablehttpresponse response = this.httpclient.execute(httpdelete); // 4 解析结果,封装返回对象httpresult,相当于显示相应的结果 // 状态码 // response.getstatusline().getstatuscode(); // 响应体,字符串,如果response.getentity()为空,下面这个代码会报错,所以解析之前要做非空的判断 // entityutils.tostring(response.getentity(), "utf-8"); httpresult httpresult = null; // 解析数据封装httpresult if (response.getentity() != null) { httpresult = new httpresult(response.getstatusline().getstatuscode(), entityutils.tostring(response.getentity(), "utf-8")); } else { httpresult = new httpresult(response.getstatusline().getstatuscode(), ""); } // 返回 return httpresult; } }
三,调用接口
package cn.xxxxxx.httpclient.test; import java.util.hashmap; import java.util.map; import org.junit.before; import org.junit.test; import cn.itcast.httpclient.apiservice; import cn.itcast.httpclient.httpresult; public class apiservicetest { private apiservice apiservice; @before public void init() { this.apiservice = new apiservice(); } // 查询 @test public void testqueryitembyid() throws exception { // http://manager.aaaaaa.com/rest/item/interface/{id} string url = "http://manager.aaaaaa.com/rest/item/interface/42"; httpresult httpresult = this.apiservice.doget(url); system.out.println(httpresult.getcode()); system.out.println(httpresult.getbody()); } // 新增 @test public void testsaveitem() throws exception { // http://manager.aaaaaa.com/rest/item/interface/{id} string url = "http://manager.aaaaaa.com/rest/item/interface"; map<string, object> map = new hashmap<string, object>(); // title=测试restful风格的接口&price=1000&num=1&cid=888&status=1 map.put("title", "测试apiservice调用新增接口"); map.put("price", "1000"); map.put("num", "1"); map.put("cid", "666"); map.put("status", "1"); httpresult httpresult = this.apiservice.dopost(url, map); system.out.println(httpresult.getcode()); system.out.println(httpresult.getbody()); } // 修改 @test public void testupdateitem() throws exception { // http://manager.aaaaaa.com/rest/item/interface/{id} string url = "http://manager.aaaaaa.com/rest/item/interface"; map<string, object> map = new hashmap<string, object>(); // title=测试restful风格的接口&price=1000&num=1&cid=888&status=1 map.put("title", "测试apiservice调用修改接口"); map.put("id", "44"); httpresult httpresult = this.apiservice.doput(url, map); system.out.println(httpresult.getcode()); system.out.println(httpresult.getbody()); } // 删除 @test public void testdeleteitembyid() throws exception { // http://manager.aaaaaa.com/rest/item/interface/{id} string url = "http://manager.aaaaaa.com/rest/item/interface/44"; httpresult httpresult = this.apiservice.dodelete(url, null); system.out.println(httpresult.getcode()); system.out.println(httpresult.getbody()); } }
以上这篇使用httpclient调用接口的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
下一篇: response对象的使用(实例讲解)