51Testing软件测试论坛
标题:
java 项目中没去 log.info (cookie),运行时却把 cookie 给 log 出来了。。。求路过...
[打印本页]
作者:
测试积点老人
时间:
2019-8-27 11:47
标题:
java 项目中没去 log.info (cookie),运行时却把 cookie 给 log 出来了。。。求路过...
java 项目中没去 log.info (cookie),运行时却把 cookie 给 log 出来了。。。求路过大神给出解决方案
直接上代码:如下是个依赖登录cookie的接口
@Test
public void test1()throws Exception{
String api = "/actapi_v2/mapi/member.memberBuy";
Map<String,String> request1 = new HashMap<>();
request1.put("account","18656560102");
request1.put("password","w123456");
Map<String,String> request = new HashMap<>();
request.put("Cookie", UserInfo.Getcookie(request1));
//上一步会打印出cookie
String response = HttpUtils.getInstance().sendHttpRequest("POST",host+api,request);
System.out.println("------------------------"+response);
}
复制代码
运行后的结果
[attach]126344[/attach]
得到cookie的方法
public static String Getcookie(Map<String,String> map) throws Exception {
String api = "/actapi_v2/mapi/user.signIn";
return HttpUtils.getInstance().getSessionCookie(host+api,map);
}
复制代码
getSessionCookie方法里
public String getSessionCookie(String url,Map<String,String>maps)throws Exception{
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
for (String key : maps.keySet()) {
nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));
}
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
CloseableHttpResponse closeableHttpResponse = httpResponse(httpPost);
Header[] headers = closeableHttpResponse.getAllHeaders();
StringBuilder stringBuilder = new StringBuilder();
for (Header header:headers){
HeaderElement[] elements = header.getElements();
for (HeaderElement element:elements){
if (element.getValue() != null && !element.getValue().isEmpty()){
stringBuilder.append(element.getName()).append("=").append(element.getValue()).append(";");
}
}
}
return new String(stringBuilder);
}
复制代码
不知道运行后为什么会打印出两次cookie?按理应该一次都出现不了的。感觉是我的log4j框架问题?
[attach]126345[/attach]
作者:
jingzizx
时间:
2019-8-28 10:09
依次查找,方法,单步调试
作者:
qqq911
时间:
2019-8-28 10:34
下断点分析
作者:
litingting0214
时间:
2019-8-28 10:48
加断点一步一步调试找找问题所在
作者:
你好浮戈
时间:
2019-8-28 13:46
加断点调试
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2