首页 > 代码库 > 接口测试中如何利用cookies保持会话

接口测试中如何利用cookies保持会话

使用cookies保持会话自己研究了下应该有两种方式:

1.保持会话的第一种方法:如果用的是同一个HttpClient且没去手动连接放掉client.getConnectionManager().shutdown(); 都不用去设置cookie的ClientPNames.COOKIE_POLICY。httpclient都是会保留cookie的

package com.wq;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * 保持会话的第一种方法:如果用的是同一个HttpClient且没去手动连接放掉client.getConnectionManager().shutdown(); 
 * 都不用去设置cookie的ClientPNames.COOKIE_POLICY。httpclient都是会保留cookie的
 * @author Redick
 * @data 2017/4/24
 */

public class CookieManage {
    
    

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Map<String, List<NameValuePair>> loginMap = new HashMap<String, List<NameValuePair>>();
        List<NameValuePair> loginList = new ArrayList<NameValuePair>();
        loginList.add(new BasicNameValuePair("username","yuechunhua"));
        loginList.add(new BasicNameValuePair("password","111111"));
        loginMap.put("http://eac.mgr.banksteel.com/login.htm", loginList);
        
        Map<String, List<NameValuePair>> actionMap = new HashMap<String, List<NameValuePair>>();
        List<NameValuePair> actionList = new ArrayList<NameValuePair>();
        actionList.add(new BasicNameValuePair("accountType","7"));
        actionMap.put("http://settlement.mgr.banksteel.com/settlement/account/list.htm", actionList);
        
        
        Map<String , String> returnMap = new HashMap<String, String>();
        returnMap=doPost(loginMap,actionMap);
        System.out.println(returnMap.toString());
        System.out.println("-----------------------------------");
    }
    
    
    public static Map<String, String> doPost(Map<String,List<NameValuePair>> loginMap,Map<String,List<NameValuePair>> actionMap){
        String loginURL ="";
        List<NameValuePair> loginNameValuePairs = null;
        String actionURL = "";
        List<NameValuePair> actionNameValuePairs = null;
        int actionStatusCode = 0;
        String actionResponseBody = "";
        Map<String, String> responseMap = new HashMap<String, String>();
        
        //获取登录的map中的url和参数
        for(String entry : loginMap.keySet()){
            loginURL = entry;
            loginNameValuePairs = loginMap.get(entry);
        }
        
        //创建一个HttpClientBuilder对象
        HttpClientBuilder httpClientBuilder = null;
        httpClientBuilder=HttpClientBuilder.create();
        //使用HttpClientBuilder对象创建httpClient对象
        CloseableHttpClient httpClient = httpClientBuilder.build();
        //调用getLoginCookies方法,传入httpClient对象,使得至始至终使用的是一个httpclient
        getLoginCookies(loginURL, loginNameValuePairs,httpClient);
        
        //此时已经获取到了cookies
        for(String actionEntry : actionMap.keySet()){
            actionURL = actionEntry;
            actionNameValuePairs = actionMap.get(actionEntry);
            //采用post方法
            HttpPost post = new HttpPost(actionURL);
            //设置body
            UrlEncodedFormEntity entity = null;
            try {
                entity=new UrlEncodedFormEntity(actionNameValuePairs,"UTF-8");
                post.setEntity(entity);
                //获取响应信息
                CloseableHttpResponse response = httpClient.execute(post);
                actionStatusCode = response.getStatusLine().getStatusCode();  
                actionResponseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
                //将获取的值对放入map中返回给调用方
                responseMap.put(actionURL, actionResponseBody);
                httpClient.close();
            }catch(IOException e){
                e.printStackTrace();
            }catch(Exception e){
                e.printStackTrace();
            }
            
        }
        return responseMap;
    }
    
    
    public static Map<String, String> getLoginCookies(String loginURL,List<NameValuePair> loginNameValuePairs,
            CloseableHttpClient httpClient){
        int statusCode =0;
        String retStr = null;
        Map<String,String> loginMap = new HashMap<String, String>();
        //采用post方法
        HttpPost post = new HttpPost(loginURL);
        //设置body
        UrlEncodedFormEntity entity = null;
        try {
            entity=new UrlEncodedFormEntity(loginNameValuePairs,"UTF-8");
            post.setEntity(entity);
            //获取响应信息
            CloseableHttpResponse response = httpClient.execute(post);
            statusCode = response.getStatusLine().getStatusCode();  
            retStr = EntityUtils.toString(response.getEntity(), "UTF-8");
            //将获取的值对放入map中返回给调用方
            loginMap.put(loginURL, retStr);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return loginMap;
    }
    

}

 

接口测试中如何利用cookies保持会话