分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

接口测试之HttpClient

发布时间:2023-09-06 01:55责任编辑:彭小芳关键词:暂无标签

一.Get请求测试

测试依赖

 ????<dependency> ???????????<groupId>org.apache.httpcomponents</groupId> ???????????<artifactId>httpclient</artifactId> ???????????<version>4.5.3</version> ???????</dependency> ???????<dependency> ???????????<groupId>log4j</groupId> ???????????<artifactId>log4j</artifactId> ???????????<version>1.2.17</version> ???????</dependency> ???????<dependency> ???????????<groupId>com.alibaba</groupId> ???????????<artifactId>fastjson</artifactId> ???????????<version>1.2.28</version> ???????</dependency>
public static String getResult(String url,Map<String, Object> queryMap,Map<String, Object> postMap){ ???????//使用方法 ???????String host = "http://localhost:8081/"; ???????//1、公共参数 ???????String appid = "2c844a8b24b047d1a4c350f07240d2dc"; ???????String appsecret = "d75203566ed744a1a6ef90b9d22c46e7"; ???????String noncestr = "32412423"; ???????String erp = "e3"; ???????String erpVersion = "1.0"; ???????// postMap.put("type", "中文"); ???????//4、求出签名和请求串 ???????String[] rets = GetSignUtils.genSign(appid, appsecret, noncestr, erp, erpVersion, ???????????????queryMap, postMap);//如果非POST请求则最后一个参数postMap为null即可 ???????System.out.println(rets[1]); ???????String httpGet = HttpClientUtil.HttpGet(host + url + "?" + rets[1]); ???????return httpGet; ???}

二.Post请求测试

 ???public static String postResult(String url,Map<String,Object> queryMap,Map<String,Object> postMap,JSONObject jsonParam){ ???????//使用方法 ???????String host = "http://localhost:8081/"; ???????//1、公共参数 ???????String appid = "2c844a8b24b047d1a4c350f07240d2dc"; ???????String appsecret = "d75203566ed744a1a6ef90b9d22c46e7"; ???????String noncestr = "32412423"; ???????String erp = "e3"; ???????String erpVersion = "1.0"; ???????String[] rets = GetSignUtils.genSign(appid, appsecret, noncestr, erp, erpVersion, ???????????????queryMap, postMap);//如果非POST请求则最后一个参数postMap为null即可 ???????String uri = host + url +"?"+ rets[1]; ???????System.out.println(uri); ???????HttpPost httpPost = new HttpPost(uri); ???????CloseableHttpClient client = HttpClients.createDefault(); ???????String respContent = null;// ???????json方式 ???????StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题 ???????entity.setContentEncoding("UTF-8"); ???????entity.setContentType("application/json"); ???????httpPost.setEntity(entity); ???????System.out.println(); ???????HttpResponse resp = null; ???????try { ??????????resp = client.execute(httpPost); ???????if(resp.getStatusLine().getStatusCode() == 200) { ???????????HttpEntity he = resp.getEntity(); ???????????respContent = EntityUtils.toString(he,"UTF-8"); ???????} ???????} catch (IOException e) { ???????????e.printStackTrace(); ???????} ???????return respContent; ???}

接口测试之HttpClient

原文地址:https://www.cnblogs.com/wangxiayun/p/9078200.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved