博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpClient获取Cookie的两种方式
阅读量:5021 次
发布时间:2019-06-12

本文共 3260 字,大约阅读时间需要 10 分钟。

转载:http://blog.csdn.net/zhangbinu/article/details/72777620

一、旧版本的HttpClient获取Cookies 

p.s. 该方式官方已不推荐使用 
使用DefaultHttpClient类实例化httpClient对象:

public static String dooPost_deprecated(String url, Map
map, String charset) { DefaultHttpClient httpClient = null; HttpPost httpPost = null; String result = null; try { httpClient = new DefaultHttpClient(); httpPost = new HttpPost(url); // 设置参数 List
list = new ArrayList
(); Iterator
> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Entry
elem = (Entry
) iterator.next(); list.add(new BasicNameValuePair(elem.getKey(), elem.getValue())); } if (list.size() > 0) { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset); httpPost.setEntity(entity); } HttpResponse response = httpClient.execute(httpPost); System.out.println(response.getStatusLine().getStatusCode()); String JSESSIONID = null; String cookie_user = null; //获得Cookies CookieStore cookieStore = httpClient.getCookieStore(); List
cookies = cookieStore.getCookies(); for (int i = 0; i < cookies.size(); i++) { //遍历Cookies System.out.println(cookies.get(i)); System.out.println("cookiename=="+cookies.get(i).getName()); System.out.println("cookieValue=="+cookies.get(i).getValue()); System.out.println("Domain=="+cookies.get(i).getDomain()); System.out.println("Path=="+cookies.get(i).getPath()); System.out.println("Version=="+cookies.get(i).getVersion()); if (cookies.get(i).getName().equals("JSESSIONID")) { JSESSIONID = cookies.get(i).getValue(); } if (cookies.get(i).getName().equals("cookie_user")) { cookie_user = cookies.get(i).getValue(); } } if (cookie_user != null) { result = JSESSIONID; } } catch (Exception ex) { ex.printStackTrace(); } return result; }

二、新版本的HttpClient获取Cookies 

使用CloseableHttpClient类实例化httpClient对象:

public static String doPost(Map
map, String charset) { CloseableHttpClient httpClient = null; HttpPost httpPost = null; String result = null; try { CookieStore cookieStore = new BasicCookieStore(); httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); httpPost = new HttpPost("http://localhost:8080/testtoolmanagement/LoginServlet"); List
list = new ArrayList
(); Iterator
> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Entry
elem = (Entry
) iterator.next(); list.add(new BasicNameValuePair(elem.getKey(), elem.getValue())); } if (list.size() > 0) { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset); httpPost.setEntity(entity); } httpClient.execute(httpPost); String JSESSIONID = null; String cookie_user = null; List
cookies = cookieStore.getCookies(); for (int i = 0; i < cookies.size(); i++) { if (cookies.get(i).getName().equals("JSESSIONID")) { JSESSIONID = cookies.get(i).getValue(); } if (cookies.get(i).getName().equals("cookie_user")) { cookie_user = cookies.get(i).getValue(); } } if (cookie_user != null) { result = JSESSIONID; } } catch (Exception ex) { ex.printStackTrace(); } return result; }

HttpClient及其jar包下载地址:

转载于:https://www.cnblogs.com/ceshi2016/p/7244608.html

你可能感兴趣的文章
ubuntu 16.04 (软件应用)-输入法
查看>>
windos7修复引导扇区
查看>>
Leetcode总结之Backtracking
查看>>
Android开发学习之路-图片颜色获取器开发(1)
查看>>
StackExchange.Redis 官方文档(一) Basics
查看>>
nupkg 之破解 nodejs+electron-packager 打包exe的解包
查看>>
Objective-C 使用 C++类
查看>>
浅谈之高级查询over(partition by)
查看>>
Notes: CRM Analytics–BI from a CRM perspective (2)
查看>>
graphite custom functions
查看>>
列出所有的属性键
查看>>
js获取请求地址后面带的参数
查看>>
[原创]使用java批量修改文件编码(ANSI-->UTF-8)
查看>>
设计模式のCompositePattern(组合模式)----结构模式
查看>>
二进制集合枚举子集
查看>>
磁盘管理
查看>>
SAS学习经验总结分享:篇二—input语句
查看>>
UIImage与UIColor互转
查看>>
RotateAnimation详解
查看>>
系统管理玩玩Windows Azure
查看>>