java httpclient 設置cookies


接口測試過程中需要保持用戶登陸狀態,這里說一下設置cookies的方法

獲取cookies首先要登陸成功,

1.創建httpclient時添加cookies

     //new一個cookiestore對象
     CookieStore cookieStore = new BasicCookieStore();
        //添加cookie
        BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "f08f076d-5490-4a72-b1eb-48b58f7ab84c");
        //放入cookiestore
        cookieStore.addCookie(cookie);
        //聲明httpclient,同時添加上cookies
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCookieStore(cookieStore)//設置Cookie
                .build();

2.在headers中添加cookies

 httpPost.addHeader("JSESSIONID","7f012e0e-488b-43ff-a031-9161b9957876");

 


免責聲明!

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



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