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的接口
  1. @Test
  2.    public void test1()throws Exception{
  3.        String api = "/actapi_v2/mapi/member.memberBuy";
  4.        Map<String,String> request1 = new HashMap<>();
  5.        request1.put("account","18656560102");
  6.        request1.put("password","w123456");
  7.        Map<String,String> request = new HashMap<>();
  8.        request.put("Cookie", UserInfo.Getcookie(request1));
  9.        //上一步会打印出cookie
  10.        String response = HttpUtils.getInstance().sendHttpRequest("POST",host+api,request);
  11.        System.out.println("------------------------"+response);
  12.    }
复制代码
运行后的结果
[attach]126344[/attach]
得到cookie的方法
  1. public static String Getcookie(Map<String,String> map) throws Exception {
  2.       String api = "/actapi_v2/mapi/user.signIn";

  3.       return HttpUtils.getInstance().getSessionCookie(host+api,map);

  4.   }
复制代码
getSessionCookie方法里
  1. public String getSessionCookie(String url,Map<String,String>maps)throws Exception{
  2.        HttpPost httpPost = new HttpPost(url);
  3.        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  4.        for (String key : maps.keySet()) {
  5.            nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));
  6.        }
  7.        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
  8.        CloseableHttpResponse closeableHttpResponse = httpResponse(httpPost);
  9.        Header[] headers = closeableHttpResponse.getAllHeaders();
  10.        StringBuilder stringBuilder = new StringBuilder();
  11.        for (Header header:headers){
  12.            HeaderElement[] elements = header.getElements();
  13.            for (HeaderElement element:elements){
  14.                if (element.getValue() != null && !element.getValue().isEmpty()){
  15.                    stringBuilder.append(element.getName()).append("=").append(element.getValue()).append(";");
  16.                }

  17.            }
  18.        }
  19.        return new String(stringBuilder);
  20.    }
复制代码
不知道运行后为什么会打印出两次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