接口自動化httpclient獲取和發送cookies信息

1.獲取cookies


    public void testGetCookies() throws IOException {
 

        HttpGet get= new HttpGet(uri);
        //HttpClient沒有獲取cookies的方法
        DefaultHttpClient client=new DefaultHttpClient();
        HttpResponse response=client.execute(get);
        result= EntityUtils.toString(response.getEntity(),"utf-8");
        //獲取cookies信息
        this.store=client.getCookieStore();
        List<Cookie>cookies=store.getCookies();
        for(Cookie cookie:cookies){
            System.out.println(cookie.getName()+":"+cookie.getValue());
        }

    

   

   

2.發送帶cookies的get請求



    public void getwithCookies() throws IOException {    
        HttpGet get=new HttpGet(uri);
        DefaultHttpClient client=new DefaultHttpClient();
        //設置cookies信息
        client.setCookieStore(this.store);
        HttpResponse response=client.execute(get);
        int statusCode=response.getStatusLine().getStatusCode();
        System.out.println(statusCode);
        if(statusCode==200){
            String result=EntityUtils.toString(response.getEntity());
            System.out.println(result);
        }


​

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章