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的形式保存到客户机的某 ...
随机推荐
- http的短连接和长连接
首先http是无状态的,这个是一定的. 然后短连接和长连接本身和客户端请求没有关系. 1.短连接:客户端请求,服务器立刻响应,服务器响应后此次http请求立刻结束. 2.长连接:客户端请求,服务器可以 ...
- Django学习---自定义分页
自定义分页 简单例子: urls.py: from django.contrib import admin from django.urls import path from django.conf. ...
- Vim配置:在win10下用vim编译运行C/C++(异步插件管理,一键运行)
为什么用Vim 重新调配vim,追求尽量简单些. 安装 官网下载 PC: MS-DOS and MS-Windows下的 For modern MS-Windows systems (starting ...
- 仿知乎日报App
1.6.Error:Execution failed for task ':app:buildInfoDebugLoader'.> Exception while doing past iter ...
- Rhythmk 一步一步学 JAVA(11)Ibatis 环境配置
1.项目文件分布. 2.example1.java: package com.rhythmk.example1; import java.io.IOException; import java.io. ...
- python:while 语句的使用方法
while语句: count = 0 while True: print(count) count += 1 if count == 10: break 实例: 计算n!,若:n = 5:则:n! = ...
- <转>杜绝 Defunct进程 僵尸进程
http://hanover.iteye.com/blog/881972 在测试基于 DirectFB+Gstreamer 的视频联播系统的一个 Demo 的时候,其中大量使用 system 调用的语 ...
- Unsupported compiler 'com.apple.compilers.llvmgcc42' selected for architecture 'armv7' Xcode5
刚刚将Xcode更新到Xcode5,一运行报如下错误: Unsupported compiler 'com.apple.compilers.llvmgcc42' selected for archit ...
- ubuntu linux常用指令(1)
序号 命令 说明 1 sudo su 从普通用户切换到root用户 2 su user 从root用户切换到普通用户 3 ls 列出当前目录的文件和目录,但是不包括隐藏文件和目录 4 ls -a 列出 ...
- 全文搜索技术—Solr
1. 学习计划 1. Solr的安装及配置 a) Solr整合tomcat b) Solr后台管理功能介绍 c) 配置中文分析器 2. 使用Solr的后台管理索引库 a) ...