package org.rx.util;

import lombok.SneakyThrows;
import okhttp3.HttpUrl;
import org.openqa.selenium.Cookie;
import org.rx.core.NQuery;
import org.rx.socks.http.HttpClient; import java.util.Date;
import java.util.Set; import static org.rx.core.Contract.require; public class CookieExtend {
public static void saveFromResponse(String url, Set<Cookie> cookieSet) {
require(url, cookieSet); HttpClient.CookieContainer.getCookieJar().saveFromResponse(HttpUrl.get(url), NQuery.of(cookieSet).select(p -> {
String domain = p.getDomain();
if (domain.startsWith(".")) {
domain = domain.substring(1);
}
okhttp3.Cookie.Builder builder = new okhttp3.Cookie.Builder().name(p.getName()).value(p.getValue())
.domain(domain)
.path(p.getPath());
if (p.getExpiry() != null) {
builder = builder.expiresAt(p.getExpiry().getTime());
}
if (p.isSecure()) {
builder = builder.secure();
}
if (p.isHttpOnly()) {
builder = builder.httpOnly();
}
return fill(builder.build(), p.getDomain());
}).toList());
} @SneakyThrows
private static okhttp3.Cookie fill(okhttp3.Cookie cookie, String domain) {
// Field field = Cookie.class.getDeclaredField("domain");
// field.setAccessible(true);
// field.set(cookie, domain);
return cookie;
} public static Set<org.openqa.selenium.Cookie> loadForRequest(String url) {
require(url); return NQuery.of(HttpClient.CookieContainer.getCookieJar().loadForRequest(HttpUrl.get(url)))
.select(p -> new org.openqa.selenium.Cookie(p.name(), p.value(), p.domain(), p.path(), new Date(p.expiresAt()), p.secure(), p.httpOnly())).toSet();
}
}
HttpServletRequest request = getCurrentRequest();
if (request == null) {
return supplier.invoke(key);
}
v = request.getAttribute(k);
if (v == null) {
request.setAttribute(k, v = supplier.invoke(k));
}

selenium.Cookie 转 okhttp3.Cookie的更多相关文章

  1. selenium webdriver 如何添加cookie

    一. webdriver中常用的cookie方法 webdriver中提供了操作cookie的相关方法: get_cookies()                  获得cookie信息 add_c ...

  2. selenium webdriver处理浏览器Cookie

    有时候我们需要验证浏览器中是否存在某个cookie,因为基于真实的cookie 的测试是无法通过白盒和集成测试完成的.WebDriver 提供了操作Cookie 的相关方法可以读取.添加和删除cook ...

  3. selenium webdriver如何添加cookie

    一. webdriver中常用的cookie方法 webdriver中提供了操作cookie的相关方法: get_cookies()                  获得cookie信息 add_c ...

  4. Selenium WebDriver- 操作浏览器的cookie

    #encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...

  5. 9. selenium+request方式的cookie绕过

    1. 首先确认POST请求的content-type类型 2. 查看cookies数据 3. 找到对应的headers数据 4. 如果是application/json,传入的json数据需要时jso ...

  6. jQuery插件 -- Cookie插件jquery.cookie.js(转)

    Cookie是网站设计者放置在客户端的小文本文件.Cookie能为用户提供很多的使得,例如购物网站存储用户曾经浏览过的产品列表,或者门户网站记住用户喜欢选择浏览哪类新闻. 在用户允许的情况下,还可以存 ...

  7. jquery.cookie.js 操作cookie实现记住密码功能的实现代码

    jquery.cookie.js操作cookie实现记住密码功能,很简单很强大,喜欢的朋友可以参考下.   复制代码代码如下: //初始化页面时验证是否记住了密码 $(document).ready( ...

  8. HostOnly Cookie和HttpOnly Cookie

    怎么使用Cookie? 通常我们有两种方式给浏览器设置或获取Cookie,分别是HTTP Response Headers中的Set-Cookie Header和HTTP Request Header ...

  9. js读写Cookie问题(Cookie存储时长、Cookie存储域)汇总

    在采集网站用户行为数据/使用js对用户行为做交互时,经常会使用到Cookie,了解Js Cookie的读写,以及一些细节,非常重要.   什么是Cookie 所谓Cookie,只是一条极为短小的信息, ...

随机推荐

  1. three dots in git

    What are the differences between double-dot “..” and triple-dot “…” in Git commit ranges? Using Comm ...

  2. QString的arg方法

    第一个参数是要填充的数字,第二个参数为最小宽度,第三个参数为进制,第四个参数为当原始数字长度不足最小宽度时用于填充的字符,如 QString name=QString("R%1C%2&quo ...

  3. POJ1426-Find The Multiple-bfs

    Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal repr ...

  4. POJ 1860 Currency Exchange (Bellman-Ford)

    题目链接:POJ 1860 Description Several currency exchange points are working in our city. Let us suppose t ...

  5. 通过生成HFile导入HBase

    要实现DataFrame通过HFile导入HBase有两个关键步骤 第一个是要生成Hfile第二个是HFile导入HBase 测试DataFrame数据来自mysql,如果对读取mysql作为Data ...

  6. upc组队赛14 As rich as Crassus【扩展中国剩余定理】

    As rich as Crassus 题目链接 题目描述 Crassus, the richest man in the world, invested some of his money with ...

  7. Fiddler之iOS手机抓包代理设置

    一.前置步骤:官网下载并安装好 二.设置iOS手机代理到windows电脑 1.打开Fiddler,点击上方Tools,进入Options,选择HTTPS,按下图设置 2.fiddler默认监听端口8 ...

  8. Springboot集成Mybatis+PageHelper

    1.Springboot项目引入mysql和mybatis的依赖: <dependency> <groupId>org.mybatis.spring.boot</grou ...

  9. luoguP1314 聪明的质监员 题解(NOIP2011)

    P1314 聪明的质监员 题目 #include<iostream> #include<cstdlib> #include<cstdio> #include< ...

  10. ELK+filebeat+redis 日志分析平台

    一.简介 ELK Stack是软件集合Elasticsearch.Logstash.Kibana的简称,由这三个软件及其相关的组件可以打造大规模日志实时处理系统. 其中,Elasticsearch 是 ...