接口測試中如何利用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;
    }
    

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM