java 获取cookie
# GetCookie.java
package com.meicai.tms;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By.ByXPath;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions; public class GetCookie { // TODO Auto-generated method stub
public static void main(String[] args) { WebDriver driver = new ChromeDriver();
driver.get("url/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); WebElement user = driver
.findElement(By.name("email"));//("//*[@id='pl_login_form']/div[2]/div[3]/div[1]/div/input"));
user.clear();
user.sendKeys("username");
WebElement password = driver.findElement(By
.name("password"));//xpath("//*[@id='pl_login_form']/div[2]/div[3]/div[2]/div/input"));
password.clear();
password.sendKeys("password"); WebElement yan =driver.findElement(By.name("code"));
yan.clear();
yan.sendKeys("9527");
/* WebElement submit = driver.findElement(By
.xpath("/html/body/div/div/div[2]/form/div[4]/div/div/button"));
submit.click();*/
Actions action = new Actions(driver);
action.sendKeys(Keys.ENTER).perform(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement dianyidian =driver.findElement(By.xpath("/html/body/div/div/div[2]/a[3]"));
System.out.println(dianyidian.getText());
dianyidian.click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} File file = new File("D:\\TmsCookie.data");
try {
// delete file if exists
file.delete();
file.createNewFile();
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
for (Cookie ck : driver.manage().getCookies()) {
bw.write(ck.getName() + ";" + ck.getValue() + ";"
+ ck.getDomain() + ";" + ck.getPath() + ";"
+ ck.getExpiry() + ";" + ck.isSecure());
bw.newLine();
}
bw.flush();
bw.close();
fw.close(); } catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("cookie write to file");
}
}
}
#CookieLogin.java package com.meicai.tms; import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.concurrent.TimeUnit; import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; public class CookieLogin { /**
* @author Young
* @param args
*/ public static void main(String[] args) {
// TODO Auto-generated method stub
//Cookies.addCookies();
WebDriver driver = new ChromeDriver();
driver.get("http://weibo.com/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
try
{
File file=new File("D:\\broswer.data");
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
String line;
while((line=br.readLine())!= null)
{
StringTokenizer str=new StringTokenizer(line,";");
while(str.hasMoreTokens())
{
String name=str.nextToken();
String value=str.nextToken();
String domain=str.nextToken();
String path=str.nextToken();
Date expiry=null;
String dt;
if(!(dt=str.nextToken()).equals(null))
{
//expiry=new Date(dt);
System.out.println();
}
boolean isSecure=new Boolean(str.nextToken()).booleanValue();
Cookie ck=new Cookie(name,value,domain,path,expiry,isSecure);
driver.manage().addCookie(ck);
}
} }
catch(Exception e)
{
e.printStackTrace();
} driver.get("http://weibo.com/");
System.out.println("登陆成功!");
} }
java 获取cookie的更多相关文章
- Java通过httpclient获取cookie模拟登录
package Step1; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.Htt ...
- js/java 获取、添加、修改、删除cookie(最全)
一.cookie介绍 1.cookie的本来面目 HTTP协议本身是无状态的.什么是无状态呢,即服务器无法判断用户身份.Cookie实际上是一小段的文本信息(key-value格式).客户端向服务 ...
- 利用Python获取cookie的方法,相比java代码简便不少
1.通过urllib库,是python的标准库,不需要另外引入,直接看代码,注意代码的缩进: # coding=UTF-8import cookielibimport urllib2 class Ry ...
- 通过js获取cookie的实例及简单分析
今天碰到一个在firefox下swfupload 上传时session不一致问题 在一个项目遇到多文件上传时,firefox下,服务器端的session获取不一致问题. 解决办法: 解决办法:将ses ...
- java对cookie的操作
java对cookie的操作比较简单,主要介绍下建立cookie和读取cookie,以及如何设定cookie的生命周期和cookie的路径问题. 建立一个无生命周期的cookie,即随着浏览器的关闭即 ...
- java之Cookie详解
Cookie是由服务器端生成,发送给User-Agent(一般是浏览器),浏览器会将Cookie的key/value保存到某个目录下的文本文件内,下次请求同一网站时就发送该Cookie给服务器(前提是 ...
- JAVA操作COOKIE
JAVA操作COOKIE 1.设置Cookie Cookie cookie = new Cookie("key", "value"); cookie.setMa ...
- 利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录
利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录 tring cookie = response.Headers.Get("Set-Cookie ...
- JSP获取Cookie对象
cookie是小段的文本信息,在网络服务器上生成,并发送给浏览器的.通过使用cookie可以标识用户身份,记录用户和密码,跟踪重复用户等.浏览器将cookie以key/value的形式保存到客户机的某 ...
随机推荐
- Oracle数据库学习笔记(一)
Oracle的体系结构大体上分为两部分:Instance(实例)和Database(数据库). Instance(实例) :在Oracle Instance中主要包含了SGA以及一些进程(例如:P ...
- wget 技巧
最近用到一个命令wget,有一个技巧分享一下. [root@py ~]# wget -m -k http://www.example.com 可以将示例网站整个打包,作为本地镜像.
- RPC通信
@version: @author: morgana @license: Apache Licence @contact: vipmorgana@gmail.com @site: @software: ...
- linux下mysql的root密码忘记----解决方案
1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 状态下 ...
- 【airflow实战系列】 基于 python 的调度和监控工作流的平台
简介 airflow 是一个使用python语言编写的data pipeline调度和监控工作流的平台.Airflow被Airbnb内部用来创建.监控和调整数据管道.任何工作流都可以在这个使用Pyth ...
- 使用ssh client与bash scripts轻松管理多台主机
当我们需要控制一个局域网中的很多台服务器时,一个简单的全局操作可能会被放大地异常繁琐,这时我们就会需要新的工具来快速完成这种工作. 我们将使用ssh客户端提供的一些工具来快速完成这一开发工作,我们的开 ...
- An Intuitive Explanation of Fourier Theory
Reprinted from: http://cns-alumni.bu.edu/~slehar/fourier/fourier.html An Intuitive Explanation of Fo ...
- Spark之 RDD转换成DataFrame的Scala实现
依赖 <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2. ...
- 详细探究Spark的shuffle实现
Background 在MapReduce框架中,shuffle是连接Map和Reduce之间的桥梁,Map的输出要用到Reduce中必须经过shuffle这个环 节,shuffle的性能高低直接影响 ...
- cs api 之一
无法创建 无法创建网络 执行顺序