第4节 获取cookies信息
//用来存储cookies信息的变量
private CookieStore store;
@Test
public void testGetCookies() throws IOException {
String result;
//从配置文件中拼接url
String uri = bundle.getString("getCookies.uri");
String testUrl = this.url+uri;
HttpGet get = new HttpGet(testUrl);
//获取DefaultHttpClient请求
DefaultHttpClient client = new DefaultHttpClient();
//执行get方法
HttpResponse response = client.execute(get);
//拿到响应结果的实体entity
result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println("result "+result);
//获取cookies信息
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);
}
}
第5节 携带cookies信息访问get请求
@Test(dependsOnMethods = {"testGetCookies"})
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(),"utf-8");
System.out.println(result);
}
}
执行结果:
在学习过程中遇到的问题:
在读取配置文件的时候总是提示:
java.util.MissingResourceException: Can't find bundle for base name application, locale zh_CN
搜索了一番都是说路径不对要写绝对路径,但是试了也不行,新建了一个项目后,配置一样路径一样再次测试就成功了,不知道之前是由于什么原因导致的,目前还没有解决