1. 程式人生 > >測試框架httpclent 3.獲取cookie的信息,然後帶cookies去發送請求

測試框架httpclent 3.獲取cookie的信息,然後帶cookies去發送請求

out post his bundle oca status post請求 throw sta

在properties文件裏面:

技術分享圖片

startupWithCookies.json

[
  {
    "description":"這是一個會返回cookies信息的get請求",
    "request":{
      "uri":"/getCookies",
      "method":"get"

    },
    "response":{
      "cookies":{
        "login":"true"
      },
      "text":"恭喜獲得cookies信息成功"
    }

  },


  {
    "description":"這是一個帶cookies的請求",
    
"request":{ "uri":"/get/with/cookies", "method":"get", "cookies":{ "login":"true" } }, "response":{ "text":"這是一個需要攜帶cookies信息才能訪問的get請求" } }, { "description":"這是一個帶cookies的post請求", "request":{ "uri":"/post/with/cookies", "method":"post",
"cookies":{ "login":"true" }, "json":{ "name":"huhanshan", "age":"18" } }, "response":{ "status":200, "json":{ "huhanshan":"success", "status":"1" } } } ]

進入moco和json文件的所在目錄:運行以下命令

java -jar ./moco-runner-0.12.0-standalone.jar http -p 8888 -c startupWithCookies.json

Java文件:

package com.course.httpclient.cookies;

import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

public class MyCookiesForGet {

    private String url;
    private ResourceBundle bundle;

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


    @BeforeTest
    public void beforeTest(){
        bundle = ResourceBundle.getBundle("application",Locale.CHINA);
        url = bundle.getString("test.url");

    }


    @Test
    public void testGetGookies() throws IOException {
        String result;
        String uri = bundle.getString("getCookies.uri");
        HttpGet get = new HttpGet(this.url + uri);
        DefaultHttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(get);

        result = EntityUtils.toString(response.getEntity(),"utf-8");
        System.out.println(result);

        //獲取cookies的信息,因為cookie裏面不只是一個,他是一個cookie類型的list
        store = client.getCookieStore();
        List<Cookie> cookieList = store.getCookies();

        for(Cookie cookie : cookieList){
            String name = cookie.getName();
            String value = cookie.getValue();
            System.out.println("name = "+name+",value = "+value);
        }
    }

    @Test(dependsOnMethods = "testGetGookies")
    public void testGetWithCookies() throws IOException {
        String uri = bundle.getString("test.get.with.cookies");
        HttpGet get = new HttpGet(this.url + uri);
        DefaultHttpClient client = new DefaultHttpClient();

        //設置cookies信息
        client.setCookieStore(store);

        HttpResponse response = client.execute(get);

        //獲取響應的狀態碼
        int statusCode = response.getStatusLine().getStatusCode();

        System.out.println("statusCode="+statusCode);

        if(statusCode==200){
            String result = EntityUtils.toString(response.getEntity(),"utf-8");
            System.out.println(result);
        }
    }

}

測試框架httpclent 3.獲取cookie的信息,然後帶cookies去發送請求