
课程咨询: 400-996-5531 / 投诉建议: 400-111-8989
认真做教育 专心促就业
上海达内教程接口测试之接口diff方法实现
一、HAR文件录制
public List<HttpInfo> HttpInfoSave(){
/*读取resources中的HAR文件*/
/*HAR文件只有一行,这一行以String类型存储至HttpArchive变量中*/
/*拆解HttpArchive这个JSON结构体*/
for(i = 0:HttpArchive中entry的数目){
/*获得这条HTTP请求*/
/*保存请求的url、用户使用的设备信息、请求方法(GET还是POST)*/
/*保存响应的文本信息*/
/*保存相应的文本编码信息*/
/*保存POST请求的数据格式信息以及请求主体信息*/
}
/*返回HttpInfo的列表*/
}
|
public void login() throws HttpException, IOException{
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("login url");
JSONObject js = new JSONObject();
js.element("username", "[hzsuixiang01@163.com](mailto:hzsuixiang01@163.com)");
js.element("accountType", "0");
js.element("auth", "d554e66e583c8946ad81ba52f6ea468d");
post.setRequestBody(js.toString());
int status = client.executeMethod(post);
System.out.println(status+post.getResponseBodyAsString());
Header[] header = post.getResponseHeaders();
cookies[0] = header[6].getValue(); //已知返回两个Set-Cookie字段,都要保存下来
cookies[1] = header[7].getValue();}
|
for(int i = 0;i<HttpInfoList.size();i++){
HttpInfo HttpInfo = HttpInfoList.get(i);
if(HttpInfo.getMethod().equals("GET")){
System.out.println("/********************开始处理"+i+"请求了哦*******************************/");
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(HttpInfo.getUrl());
get.addRequestHeader("X-User-Agent",HttpInfo.getUserAgent());
get.addRequestHeader("Cookie", cookies[0]);
get.addRequestHeader("Cookie", cookies[1]);
int status = client.executeMethod(get);
System.out.println(get.getResponseBodyAsString());
if(HttpInfo.getEncoding().equals("base64")){
byte[] content = Base64.decodeBase64(HttpInfo.getResponseContent());
String contentString = new String(content);
System.out.println(contentString); }
else{
System.out.println(HttpInfo.getResponseContent());
System.out.println(HttpInfo.getResponseContent().equals(get.getResponseBodyAsString()));
}
}else{
System.out.println("/********************开始处理"+i+"请求了哦*******************************/");
MyClient myClient = new MyClient();
if(HttpInfoList.get(i).getMimeType().equals("application/x-[www-form-urlencoded]{
System.out.println("primitive result: "+HttpInfoList.get(i).getResponseContent());
myClient.doPostWithDataPair(HttpInfoList.get(i).getUrl(), cookies, HttpInfoList.get(i).getParamsPair());
}
else{
System.out.println("primitive result: "+HttpInfoList.get(i).getResponseContent());
myClient.doPostWithDataJson(HttpInfoList.get(i).getUrl(), cookies, HttpInfoList.get(i).getParamsJson());
}
}
}
|