TA的每日心情 | 擦汗 9 小时前 |
---|
签到天数: 527 天 连续签到: 4 天 [LV.9]测试副司令
|
1测试积点
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);
- }
复制代码 运行后的结果
得到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框架问题?
|
|