标签:app mes gets 用法 exec highlight 新建 equals orm
多次理解,反复练习,破釜沉舟。
Apache Jakarta Common 下的子项目
支持 HTTP 协议的客户端编程工具包
支持 HTTP 协议最新的版本
简化重点:
1、接口请求与接口响应(先要理解http协议),常见的以下两种请求方式
Get
Post
2、数据解析,学会以下用法
JsonObject
JsonArray
3、结果断言
这里以https://my.oschina.net/u/3559695/blog/1600534/网页的接口为例 1 @Test
2 public void test1() throws IOException { 3 // 构造数据 4 String cookie = "111111";//会定时更新,自己去获取吧~ 7 //json的写法 => Content-Type:application/json (传输的数据格式) 8 // User user=new User();//需要新建一个User类,命名属性的set方法 9 // user.setUserId(3559695); 10 // user.setSkillsNum(5); 11 // String jsonString=JSON.toJSONString(user); 等同于 String jsonString=" {\"userId\":3559695,\"skillsNum\":5}";
12 // System.out.println("打印"+jsonString); 13 // StringEntity stringEntity=new StringEntity(jsonString); 14 15 //form的写法 => Content-Type:application/x-www-form-urlencoded 16 //携带普通的参数params的方式 17 List<NameValuePair> params=new ArrayList<>(); 18 params.add(new BasicNameValuePair("userId","3559695"));//BasicNameValuePair是存储键值对的类 19 params.add(new BasicNameValuePair("skillsNum","5")); 20 String jsonString=EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));//这里就是:userId=3559695&skillsNum=5 21 System.out.println(jsonString); 22 23 //1、打开浏览器 24 CloseableHttpClient httpClient = HttpClients.createDefault(); 25 //2、创建httpGet对象 26 HttpPost httpPost = new HttpPost("https://my.oschina.net/u/3559695/radar/getUserPortraitRadarMap"); 27 httpPost.addHeader("Cookie", cookie); 28 // httpPost.addHeader("Accept-Encoding","gzip, deflate, br"); 29 httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded"); 30 httpPost.addHeader("User-Agent", 31 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36"); 32 // httpPost.setEntity(stringEntity); 这个是json格式的方式 33 httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8")); 34 //3、发送请求,获取响应模型 35 CloseableHttpResponse httpResponse = httpClient.execute(httpPost); 36 //4、从响应模型获取响应实体 37 HttpEntity httpEntity = httpResponse.getEntity(); 38 //5、将响应实体转为String 39 if (httpEntity != null){ 40 String str = EntityUtils.toString(httpEntity); 41 System.out.println(str); 42 //6、String解析为JsonObject、JsonArray 43 JSONObject jsonObject=JSONObject.parseObject(str); 44 System.out.println(jsonObject); 45 System.out.println("输出状态码code:"+jsonObject.getString("code")); 46 System.out.println(jsonObject.get(1)); 47 JSONObject jsonObject1=jsonObject.getJSONObject("result"); 48 System.out.println(jsonObject1); 49 JSONArray jsonArray=JSONArray.parseArray(jsonObject1.getString("skills")); 50 System.out.println(jsonArray); 51 System.out.println(jsonArray.get(1)); 52 53 //测试 54 Assert.assertEquals(jsonObject.getString("code"),"1"); 55 Assert.assertTrue(jsonArray.get(1).equals("社区影响力")); 56 Assert.assertNotNull(jsonObject.getString("result")); 57 Assert.assertNotSame(jsonObject1.getString("skills"),jsonArray); 58 59 }else EntityUtils.consume(httpEntity); 60 //释放资源 61 httpResponse.close(); 62 httpClient.close(); 63 }
url响应结果:
{"code":1,"message":"success","result":{"skills":["社区活跃度","社区影响力","技术贡献度","活动活跃性","开源贡献度","学习积极性"],"maxSkillScore":100,"skillsScore":[11.932739749333637,10.202996646175928,10.279304325337817,5.0,5.0,12.192893401015228]},"time":"2019-11-21 12:45:10"}
【接口测试】HttpClient&fastJson 总结与案例
标签:app mes gets 用法 exec highlight 新建 equals orm
原文地址:https://www.cnblogs.com/fatCat1/p/11904954.html