本文总结如何用fiddler和postman调试接口,并用java模拟调用接口。
- fiddler用法
当页面点击事件后,在fiddler出现一个请求,单击左侧请求,在右侧的raw的tab标签,出现该请求的详细内容。
其实一个请求需要两类参数,url请求、headers和body参数,放在postman做模拟请求。
#url请求
POST http://XXXXXX/front/sh/querysr!execute?uid=queryComplexList HTTP/1.1
#header参数
Host: 192.168.1.1
Connection: keep-alive
Content-Length: 3039
Accept: application/json, text/javascript,
X-Requested-With: XMLHttpRequest
_log4xContextKey: 70AB0BEC5AADD4B406180E
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Origin: 192.168.1.1
Referer: http://XXXXXX/src/module/basesr/query/ComplexQuery.html
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
Cookie: sessionid=7c95fded-6ae8-4f29-b488-75eb30be9ea0;
#body参数
wrkfmTypeCd=3&quickQuery=&busiType=&brdnetAcct=&cmplntsAddr=&cmpgnNm=&rltSysType=&wrkfmShowSwftno=&acptTime=2022-12-01+00%3A00%3A00&endTime=2022-12-31+23%3A59%3A59&acptNum=&acptStaffNum=&arcTimeStart=&arcTimeEnd=&srvReqstTypeId=401002006
- postman用法
1)根据fiddler的3类参数,分别填到postman三个框
2)post类型的body中可以存放任意的内容格式,浏览器可以根据请求头中指定的content-type类型对请求体进行解析
其对应关系如下:
序号 | body | Content-Type | 备注 |
1 | form-data | multipart/form-data | 它将表单的数据组织成Key-Value形式,用分隔符boundary(boundary可任意设置)处理成一条消息。由于有boundary隔离,所以既可以上传文件,也可以上传参数。 |
2 | x-www-form-urlencoded | application/x-www-from-urlencoded | application/x-www-form-urlencoded是默认的MIME内容编码类型,它在传输比较大的二进制或者文本数据时效率极低。只能上传键值对,不能用于文件上传。不同的field是用&区分开的 |
3 | raw | application/json | 可以上传任意格式的文本,可以上传text、json、xml、html等 |
4 | binary | application/octet-stream | 只可以上传二进制数据,通常用来上传文件。由于没有键值,所以一次只能上传一个文件 |
- java编写util类
新建一个基础类,将header,data,cooker,url等参数,声明私有;并用对应的函数写入这些私有变量。最后用formPost将所有参数聚合执行。
@Slf4j
@Component
public class baseQuery{
private Map<String,String> jsonHeader = new HashMap<>();//头文件
private Map<String,String> jsonData = new HashMap<>();//查询参数
private Map<String,String> jsonCooker = new HashMap<>();//cookie
private JSONObject jsonBody = new JSONObject();//body
private String url = "";
public Map<String,String> generateFormHeaders(JSONObject object){
jsonHeader.clear();
jsonHeader.put("Accept", "application/json, text/javascript, */*; q=0.01");
jsonHeader.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
jsonHeader.put("x-expect-language", "zh-CN");
jsonHeader.put("X-Requested-With", "XMLHttpRequest");
jsonHeader.put("Accept-Language", "zh-CN,zh;q=0.9");
jsonHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36");
jsonHeader.put("Connection", "keep-alive");
for (String key: object.keySet()) {
String value = object.getString(key);
if (!StringUtils.isEmpty(key)) {
jsonHeader.put(key, value);
}
}
return jsonHeader;
}
public Map<String, String> generateFormData(JSONObject object) {
jsonData.clear();
for (String key: object.keySet()) {
String value = object.getString(key);
if (!StringUtils.isEmpty(key)) {
jsonData.put(key, value);
}
}
return jsonData;
}
public Map<String, String> generateCookie(JSONObject object) {
jsonCooker.clear();
for(String key : object.keySet()) {
String value = object.getString(key);
if(!StringUtils.isEmpty(key)) {
jsonCooker.put(key, value);
}
}
return jsonCooker;
}
public JSONObject generateBody(JSONObject object) {
jsonBody.clear();
for(String key : object.keySet()) {
String value = object.getString(key);
if(!StringUtils.isEmpty(key)) {
jsonBody.put(key, value);
}
}
jsonBody = object;
return jsonBody;
}
public String generateUrl(String strUrl) {
url = strUrl;
return url;
}
public String formPost() {
Connection conn = Jsoup.connect(url);
conn.ignoreContentType(true);
conn.method(Method.POST);
conn.followRedirects(true);
//设置头部
conn.headers(jsonHeader);
//设置请求内容
conn.data(jsonData);
//body
conn.requestBody(JSON.toJSONString(jsonBody));
//cookie
conn.cookies(jsonCooker);
String resp = "操作失败";
try {
Response response = conn.timeout(600).execute();
resp = response.body();
} catch (Exception e) {
log.debug(e.getLocalizedMessage()+jsonData.toString());
// TODO Auto-generated catch block
// logger.error(ExceptionUtils.getStackTrace(e));
}
return resp;
}
}