HttpClient学习笔记(获取cookies)

本篇文章是在这篇文章上进行拓展

1.获取body部分

String result;
HttpGet get = new HttpGet(testUrl);   //testUrl在上文做过拼接,具体地址是http://localhost:8899/getcookies
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);

这部分打印出来的结果

2.获取cookies

CookieStore store = client.getCookieStore();     //新建一个store变量,用于获取clinet中的cookies信息
List<Cookie> cookieList = store.getCookies();    //将store的数据保存为List

for (Cookie cookie : cookieList){      //for循环,为所有cookie的key和value赋值
      String name =cookie.getName();
      String value = cookie.getValue();
      System.out.println("cookie name = "+name+", cookie value = "+value);  //打印结果

输出结果,获取cookie信息成功 “login”:”true”

猜你喜欢

转载自blog.csdn.net/lt326030434/article/details/80448937