HttpClient发送cookie

public static HttpResponse post(String url,Map paraMap)throws Exception, IOException{

//用fillder取出登陆响应信息,获取cookie信息

String setCookie1="pcpopclub=092093fabcdf45erea2f8f2d4bbbd9156049a959e; domain=.autohome.com.cn; expires=Thu, 08-Aug-2075 14:08:18 GMT; path=/;";

String pcpopclub = setCookie1.substring("pcpopclub=".length(), setCookie1.indexOf(";"));

String setCookie2="clubUserShow=12345678|110|2|%e9%94%90%e5%93%a5%e5%be%88%e4%ba%8c|0|0|0||2018-07-25 14:08:18|0; domain=.autohome.com.cn; expires=Thu, 08-Aug-2075 14:08:18 GMT; path=/";

String clubUserShow = setCookie2.substring("clubUserShow=".length(), setCookie2.indexOf(";"));

//新建cookie存储cookie

CookieStore cookieStore =new BasicCookieStore();

BasicClientCookie cookie1 =new BasicClientCookie("pcpopclub", pcpopclub);

cookie1.setVersion(0);

cookie1.setDomain(".autohome.com.cn");

cookie1.setPath("/");

cookie1.setAttribute(BasicClientCookie.PATH_ATTR,"/");

cookie1.setAttribute(BasicClientCookie.DOMAIN_ATTR,".autohome.com.cn");

BasicClientCookie cookie2 =new BasicClientCookie("clubUserShow", clubUserShow);

cookie2.setVersion(0);

cookie2.setDomain(".autohome.com.cn");

cookie2.setPath("/");

cookie2.setAttribute(BasicClientCookie.PATH_ATTR,"/");

cookie2.setAttribute(BasicClientCookie.DOMAIN_ATTR,".autohome.com.cn");

cookieStore.addCookie(cookie1);

cookieStore.addCookie(cookie2);

//创建httpclient发送cookiestore

HttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();

HttpPost post=new HttpPost(url);

List paramsList =new ArrayList();

Set keySet = paraMap.keySet();

for (String key : keySet) {

paramsList.add(new BasicNameValuePair(key, paraMap.get(key)));

}

UrlEncodedFormEntity entitys =new UrlEncodedFormEntity(paramsList,Consts.UTF_8);

post.setEntity(entitys);

HttpResponse httpResponse = httpclient.execute(post);

return httpResponse;

}

你可能感兴趣的:(HttpClient发送cookie)