Set<Cookie> allCookies = driver.manage().getCookies();
try {
CookieStore cookiestore = new BasicCookieStore();
for (@SuppressWarnings("rawtypes")
Iterator iterator = allCookies.iterator(); iterator.hasNext();) {
Cookie cookie = (Cookie) iterator.next();
BasicClientCookie bcookie = new BasicClientCookie(cookie.getName(), cookie.getValue());
bcookie.setDomain(cookie.getDomain());
bcookie.setExpiryDate(cookie.getExpiry());
bcookie.setPath(cookie.getPath());
cookiestore.addCookie(bcookie);
} new File(cookieSavePath).mkdirs();
File file = new File(cookieSavePath + "/cookie.file" + cookieNumber++);
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);// 写入的文件是以二进制文件存储
System.out.println("cookile:" + cookiestore);
oos.writeObject(cookiestore);
oos.close();
fos.close(); // 读cookie
if (file.exists()) {
FileInputStream fin = null;
try {
fin = new FileInputStream(file);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
CookieStore cookieStore = null;
ObjectInputStream in;
try {
in = new ObjectInputStream(fin);
cookieStore = (CookieStore) in.readObject();
System.out.println(cookieStore);
in.close();
} catch (IOException e) { } catch (ClassNotFoundException e) { }
// System.out.println(cookieStore);
List<org.apache.http.cookie.Cookie> l = cookieStore.getCookies();
for (org.apache.http.cookie.Cookie temp1 : l) {
Cookie cookie = new Cookie(temp1.getName(), temp1.getValue(), temp1.getDomain(),
temp1.getPath(), temp1.getExpiryDate(), false);
System.out.println(cookie);
} } } catch (IOException e) {
System.out.println("IOException,add " + temp + " to uselessList!");
uselessList.add(temp);
}
private static String GetCookies() {
String cookieStr="";
File file = new File(cookiePath + "/cookie.file1");
// 读cookie
if (file.exists()) { FileInputStream fin = null;
try {
fin = new FileInputStream(file);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
CookieStore cookieStore = null;
ObjectInputStream in;
try {
in = new ObjectInputStream(fin);
cookieStore = (CookieStore) in.readObject();
System.out.println(cookieStore);
in.close();
} catch (IOException e) {
System.out.println(e); } catch (ClassNotFoundException e) {
System.out.println(e); }
List<org.apache.http.cookie.Cookie> l = cookieStore.getCookies();
String tempstr="";
for (org.apache.http.cookie.Cookie temp1 : l) {
Cookie cookie = new Cookie(temp1.getDomain(),temp1.getName(), temp1.getValue(), temp1.getPath(),
temp1.getExpiryDate(), false);
tempstr=cookie.toString().substring(0, cookie.toString().indexOf("domain"));
cookieStr+=tempstr;
System.out.println(tempstr);
}
}
cookieStr=cookieStr.substring(0,cookieStr.length()-1);
System.out.println(cookieStr);
return cookieStr;
}

CookieStore之Cookie的获取与保存的更多相关文章

  1. Python爬虫之cookie的获取、保存和使用【新手必学】

    前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:huhanghao Cookie,指某些网站为了辨别用户身份.进行ses ...

  2. selenium常用操作,查找元素,操作Cookie,获取截图,获取窗口信息,切换,执行js代码

    目录: 1. 常用操作 2. 查找元素 3. 操作Cookie 4. 获取截图 5. 获取窗口信息 6. 切换 7. 执行JS代码 简介 selenium.webdriver.remote.webdr ...

  3. C#使用FFMPEG推流,并且获取流保存在本地,随时取媒体进行播放!

    最近开发了基于C#的推流器一直不大理想,终于在不懈努力之后研究了一点成果,这边做个笔记:本文着重在于讲解下如何使用ffmpeg进行简单的推流,看似简单几行代码没有官方的文档很吃力.并获取流的源代码:如 ...

  4. 设置cookie,获取cookie,删除cookie,修改cookie

    怎么设置cookie,怎么设置cookie以及删除cookie和cookie详解 在操作cookie之前,先来看一下cookie长什么样. 可以看到,cookie是一个个键值对(“键=值”的形式)加上 ...

  5. 【VBA】获取模板保存的路径

    使用VBA如何获取模板保存的路径呢?具体代码如下: Sub 获取Excle模板保存路径() MsgBox "获取Excle模板保存路径:" & Application.Te ...

  6. c# 使用网站的身份验证及 Cookie 的获取与使用

    C# 的 Http 访问可以使用 .net 自带的  HttpWebRequest, WebClient, HttpClient 类.也可以使用开源库 RestSharp . RestSharp 的优 ...

  7. js读取cookie 根据cookie名称获取值、赋值

    借鉴:原作者https://blog.csdn.net/zouxuhang/article/details/80548417   //方法1   //存在问题:如果cookie中存在 aaaname= ...

  8. MFC_VC++_时间获取与保存列表控件内容到文件操作方法

    MFC_VC++_时间获取与保存列表控件内容到excel文件操作方法 void CDataView::OnBnClickedBtnExporttoexcel() { CTime time = CTim ...

  9. js读取cookie 根据cookie名称获取值的方法

    //方法1 //存在问题:如果cookie中存在 aaaname=aa;name=bb 获取name的值就会出现错误function getCookie(c_name){ if (document.c ...

随机推荐

  1. Linux学习笔记(二)2015.4.14

    笔记2.1 Q:登陆命令  A:刚开始登陆的是安装Linux时设置的普通用户,如lin,输入su - root后,成为root用户 笔记2.2 Q:切换控制台  A:ctrl+alt+F1-F7可以切 ...

  2. Unreal模型文件调查

    http://en.wikipedia.org/wiki/UE_Viewer http://www.gildor.org/en/projects/umodel http://wiki.beyondun ...

  3. VIM辅导:视频教程,文档资料,经典插件

    VIM辅导:25个vim视频' 教程 '资源   转自: http://blog.jobbole.com/10250/ 编注:@程序员的那些事 12月14日在新浪微博发起的<你最常用哪些文本编辑 ...

  4. tomcat 无法加载js和css 等静态文件的问题

    前段时间做了个网站,在本地tomcat测试都没有问题,但是部署到阿里云上之后,系统样式全没了.jsp等动态页面访问正常. 打开浏览器监控发现所有的css 和js 文件返回都是404 .直接访问单个的c ...

  5. Outlook2003收到的邮件不能显示图片,但转发或回复可以 故障排错

    转自:http://wenku.baidu.com/link?url=4O0Wpsnk_OasA7B-M4KMJ4H-WnLicT4vfVOT7AvXhTjpn-p2gBTNsXR4W230r0gAE ...

  6. MySQL里的found_row()与row_count()的解释及用法

    MySQL中有两个函数来计算上一条语句影响了多少行,不同于SqlServer/Oracle,不要因为此方面的差异而引起功能问题   出处:mysqlpub.com MySQL中有两个函数来计算上一条语 ...

  7. hadoop常见问题汇集

    1 hadoop conf.addResource http://stackoverflow.com/questions/16017538/how-does-configuration-addreso ...

  8. Java学习笔记(二)不定时更新

    Java语言画图 package cn.witksy.dev; import javax.imageio.ImageIO; import java.awt.*; import java.awt.ima ...

  9. LAMP环境CentOS6.4 PHP5.4随笔未整理

    首先安装一些辅助的软件或者说是依赖的关系包. 1.安装libxml2: libxml是一个用来解析XML文档的函数库.它用C语言写成, 并且能为多种语言所调用,例如C语言,C++,XSH.C#, Py ...

  10. 【Solr】solr的增删改查

    目录 创建工程 增 删 改 查 高量查询 回到顶部 创建工程 普通的java web工程即可,我采用的是spring mvc! 回到顶部 增 @Autowired private SolrServer ...