【接口測試】使用httpClient獲取cookies+攜帶獲取的cookies訪問get接口


數據准備

在本機或者遠端機器安裝部署moco-runner(參考:https://blog.csdn.net/qq_32706349/article/details/80472445

這里我們只需要准備Json文件:

[
{
    "description":"這是一個獲取cookies信息的get請求",
    "request":{
      "uri":"/getcookies",
      "method":"get"
    },
    "response":{
      "cookies":{
        "login":"true"
      },
      "text":"獲得cookies信息成功"
    }
  },
  {
    "description":"這是一個帶cookies信息的get請求",
    "request":{
      "uri":"/get/with/cookies",
      "method":"get",
      "cookies":{
        "login":"true"
      }
    },
    "response":{
      "text":"這是一個需要攜帶cookies信息才能訪問的get請求"
    }
  }
]

  代碼實現

  

import org.apache.http.HttpEntity;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.*;

public class CookieTest {

    //用來存儲Cookies信息的變量
    private CookieStore store;

//調用獲取cookie信息的get接口
    @Test
    public void testGetCookies() throws IOException {
        //獲取body
        String result=null;
        store = new BasicCookieStore();
        CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(store).build();
        HttpGet get=new HttpGet("http://localhost:30080/getcookies");
        CloseableHttpResponse response=client.execute(get);
        HttpEntity entity = response.getEntity();
        result= EntityUtils.toString(entity,"utf-8");
        System.out.println(result);

        List<Cookie> cookieList=store.getCookies();
        for (Cookie cookie:cookieList){
            String name =cookie.getName();
            String value = cookie.getValue();
            System.out.println("訪問/getcookies接口成功,cookie name = "+name+", cookie value = "+value);
        }
        response.close();
        client.close();
    }
//調用帶cookie信息的get接口
//方式一:
//通過創建cookieStore存放cookie,以此傳遞到httpclient
    @Test
    public void testGetWithCookies1() throws IOException {
        //創建cookieStore存儲cookie
        CookieStore cookieStore=new BasicCookieStore();
        //創建cookie對象
        BasicClientCookie cookie=new BasicClientCookie("login","true");
        cookie.setDomain("localhost");
        cookie.setPath("/");
        System.out.println(cookie);
        cookieStore.addCookie(cookie);

        CloseableHttpClient client=HttpClients.custom().setDefaultCookieStore(cookieStore).build();
        HttpGet get=new HttpGet("http://localhost:30080/get/with/cookies");
        CloseableHttpResponse response=client.execute(get);
        HttpEntity entity=response.getEntity();
        String result=EntityUtils.toString(entity,"utf-8");
        System.out.println(result);

        response.close();
        client.close();
    }
    //方式二:
    @Test
    public void testGetWithCookies2() throws IOException {
       CloseableHttpClient client= HttpClients.createDefault();
       HttpGet get=new HttpGet("http://localhost:30080/get/with/cookies");
       get.setHeader("cookie","login=true");
       CloseableHttpResponse response=client.execute(get);
       HttpEntity entity=response.getEntity();
       String result=EntityUtils.toString(entity,"utf-8");
       System.out.println(result);

       response.close();
       client.close();
   }
      //方式三:
      //依賴獲取cookie接口,store傳遞cookie
   @Test(dependsOnMethods = {"testGetCookies"})
    public void testGetWithCookies3() throws IOException {
        CloseableHttpClient client=HttpClients.custom().setDefaultCookieStore(this.store).build();
        HttpGet get=new HttpGet("http://localhost:30080/get/with/cookies");
        CloseableHttpResponse response=client.execute(get);
        HttpEntity entity=response.getEntity();
        String result=EntityUtils.toString(entity,"utf-8");
        System.out.println(result);

        response.close();
        client.close();
    }
}

  

總結一下遇到的問題:

1、網上教程大多是使用舊的DefaultHttpClient實現獲取cookie,但是httpclient新版本已經不推薦,這里使用的是CloseableHttpClient通過setDefaultCookieStore()方式傳遞cookie

舊的實現方式參考:https://blog.csdn.net/lt326030434/article/details/80449856

新的實現方式參考:https://blog.csdn.net/wsrfljygracie/article/details/89181318

2、在cookie傳遞的使用方面,找了一些資料,不停調試測試,靠着IDEA的提示和資料調通。

使用CookieStore保持會話的使用方法參考:https://www.cnblogs.com/ssgao/p/8829056.html

需要注意:cookie需要設置name、value、domain(這個沒有設置會一直調不通)

3、注意moco-runner部署的服務器地址,如果是測試服務器本機就是localhost,如果是遠端機器,url需要填寫具體的url地址

4、代碼實現是比較簡單的遠端調用接口的形式,沒有使用本地resources目錄配置下application.properties、foo.json,后續會學習。

另外,個人感覺json放在本地資源文件調用的必要性不是很大,看工具的使用范圍,自用部署在服務器即可,也不需要每次修改后提交代碼。

 

轉發請說明出處,謝謝。

如果還有其他問題,歡迎交流。


免責聲明!

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



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