1、测试类

package httpclient;

import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
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");


    }
    //没有cookie
    @Test
    public void testGetCookies() throws IOException {
        String result;
        //从配置文件中拼接测试url
        String uri = bundle.getString("getCookees.uri");
        String testUrl = this.url+uri;
        HttpGet get = new HttpGet(testUrl);
        HttpClient client = new DefaultHttpClient();
        HttpResponse httpResponse= client.execute(get);
        result = EntityUtils.toString(httpResponse.getEntity());
        System.out.println(result);
    }
    //获取cookie
    @Test
    public void testGetCookies1() throws IOException {
        String result;
        //从配置文件中拼接测试url
        String uri = bundle.getString("getCookees.uri");
        String testUrl = this.url+uri;
        HttpGet get = new HttpGet(testUrl);
        DefaultHttpClient client = new DefaultHttpClient();
        HttpResponse httpResponse= client.execute(get);
        result = EntityUtils.toString(httpResponse.getEntity());
        System.out.println(result);

        //获取cookie信息
        this.store  = client.getCookieStore();
        List<Cookie> cookieList = store.getCookies();
        for (Cookie cookie:cookieList
             ) {
            String name = cookie.getName();
            String value = cookie.getValue();
            System.out.println("cookie name"+name+"cookie value"+value);
        }
    }
    @Test(dependsOnMethods = "testGetCookies1")
    public void testGetWithCookies() throws IOException {
        String uri = bundle.getString("test.get.with.cookies");
        String testUrl = this.url+uri;
        HttpGet get = new HttpGet(testUrl);
        DefaultHttpClient client = new DefaultHttpClient();
        //设置cookies信息
        client.setCookieStore(this.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());
            System.out.println(result);
        }

    }
}
测试类

相关文章:

  • 2021-06-19
  • 2021-11-05
  • 2021-12-29
  • 2022-12-23
  • 2021-12-10
  • 2022-12-23
  • 2022-12-23
  • 2021-06-27
猜你喜欢
  • 2022-12-23
  • 2018-05-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-02
  • 2021-06-04
相关资源
相似解决方案