Bootstrap

java获取cookie不全_用httpclient获取到的cookie不完整是怎么回事? 原创

如果网址是百度的话获取不到cookie,其它网站的话只能获取一个或2个,看了一下应该只能获取响应的信息吧。是不是代码有哪些不足呢 请大神指点。

package com.yutian.test;

import java.io.IOException;

import java.util.List;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.cookie.Cookie;

import org.apache.http.impl.client.BasicCookieStore;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

public class GetCook {

public static void main(String[] args) throws Exception {

BasicCookieStore cookieStore = new BasicCookieStore();

CloseableHttpClient chc = HttpClients.custom().setDefaultCookieStore(cookieStore).build();

String loginUrl = "http://baidu.com";

HttpGet get = new HttpGet(loginUrl);

get.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

get.setHeader("Accept-Encoding","gzip, deflate");

get.setHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");

get.setHeader("Connection", "keep-alive");

get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0");

try {

HttpResponse response = chc.execute(get);

HttpEntity entity = response.getEntity();

EntityUtils.consume(entity);

List list = cookieStore.getCookies();

System.out.println(list.size());

if(list.isEmpty())

{

System.out.println("不存在");

}

else

{

for (int i = 0; i < list.size(); i++) {

System.out.println("-"+list.get(i).toString());

}

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

;