okhttp添加自定义cookie
package cn.x.request; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import okhttp3.Call;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response; public abstract class BaseRequest {
String url;
String requestMethod;
OkHttpClient okHttpClient ;
RequestBody body ;
Request request;
Call call;
Response response; public BaseRequest() {
final HashMap<String, List<Cookie>> cookieStore = new HashMap<>();
this.okHttpClient = new OkHttpClient.Builder()
.cookieJar(new CookieJar() {
@Override
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
cookieStore.put(httpUrl.host(), list);
} @Override
public List<Cookie> loadForRequest(HttpUrl httpUrl) {
List<Cookie> cookies = cookieStore.get(httpUrl.host());
return cookies != null ? cookies : new ArrayList<Cookie>();
}
})
.build();
} public abstract void prepareRequest() throws Throwable;
public abstract Response excuteRequest() throws Throwable; public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
} public String getRequestMethod() {
return requestMethod;
} public void setRequestMethod(String requestMethod) {
this.requestMethod = requestMethod;
} public OkHttpClient getOkHttpClient() {
return okHttpClient;
} public void setOkHttpClient(OkHttpClient okHttpClient) {
this.okHttpClient = okHttpClient;
} public RequestBody getBody() {
return body;
} public void setBody(RequestBody body) {
this.body = body;
} public Request getRequest() {
return request;
} public void setRequest(Request request) {
this.request = request;
} public Call getCall() {
return call;
} public void setCall(Call call) {
this.call = call;
} public Response getResponse() {
return response;
} public void setResponse(Response response) {
this.response = response;
} }
package cn.x.request; import java.util.List; import cn.x.common.AppHeader;
import okhttp3.Cookie;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response; public class JsonRequest extends BaseRequest {
String jsonString;
String accessToken; public JsonRequest(String url ,String jsonString) {
super();
this.url = url;
this.jsonString = jsonString; } @Override
public void prepareRequest() throws Throwable {
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
AppHeader appHeader = new AppHeader(jsonString);
body = RequestBody.create(JSON, jsonString);
request = new Request.Builder()
.url(url)
.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36")
.addHeader("SIGN",appHeader.getSign())
.addHeader("T",appHeader.getTimespan())
.post(body)
.build();
if( null != accessToken) {
List<Cookie> cookieList = okHttpClient.cookieJar().loadForRequest(HttpUrl.get(url));
Cookie cookie = new Cookie.Builder().name("accessToken").value(accessToken).domain(HttpUrl.get(url).host()).build();
cookieList.add(cookie);
okHttpClient.cookieJar().saveFromResponse(HttpUrl.get(url), cookieList);
}
} @Override
public Response excuteRequest() throws Throwable {
System.out.println();
if(request.header("Authorization") != null) {
System.out.println("Authorization: " + request.header("Authorization"));
}
System.out.println("url:" + url);
System.out.println("request:" + jsonString);
call = okHttpClient.newCall(request);
response = call.execute();
System.out.println("response:" + response.toString());
return response;
} public String getJsonString() {
return jsonString;
} public void setJsonString(String jsonString) {
this.jsonString = jsonString;
} public String getAccessToken() {
return accessToken;
} public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
} }
okhttp添加自定义cookie的更多相关文章
- 使用OKHttp模拟登陆知乎,兼谈OKHttp中Cookie的使用!
本文主要是想和大家探讨技术,让大家学会Cookie的使用,切勿做违法之事! 很多Android初学者在刚开始学习的时候,或多或少都想自己搞个应用出来,把自己学的十八般武艺全都用在这个APP上,其实这个 ...
- Android OkHttp的Cookie自己主动化管理
Android中在使用OkHttp这个库的时候.有时候须要持久化Cookie,那么怎么实现呢.OkHttp的内部源代码过于复杂,不进行深究.这里仅仅看当中的HttpEngineer里面的部分源代码,在 ...
- 【转】python requests库添加自定义cookie的方法
requests库是个很方便的爬虫,相关文档已经很详细了.不过我今天在爬网页时,有一个网站是在脚本中添加cookie的,但我向requests.cookies里添加cookie费了不少周折.尝试了多个 ...
- OkHttp与Cookie及Cookie的持久化
http://blog.csdn.net/u011150924/article/details/52780931 http://blog.csdn.net/chen19960724/article/d ...
- [Network] okhttp3与旧版本okhttp的区别分析
cp from : https://www.jianshu.com/p/4a8c94b239b4 1.包名改变 包名改了由之前的 com.squareup.http.改为 okhttp3. 我们需要将 ...
- Selenium WebDriver- 操作浏览器的cookie
#encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...
- Cookie、Session、Token那点事儿
1.什么是Cookie? Cookie 技术产生源于 HTTP 协议在互联网上的急速发展.随着互联网时代的策马奔腾,带宽等限制不存在了,人们需要更复杂的互联网交互活动,就必须同服务器保持活动状态(简称 ...
- Cookie、Session、Token那点事儿和前后端分离之JWT用户认证
(两篇文章转自:https://www.jianshu.com/p/bd1be47a16c1:https://www.jianshu.com/p/180a870a308a) 什么是Cookie? Co ...
- Android 网络请求框架Retrofit
Retrofit是Square公司开发的一款针对Android网络请求的框架,Retrofit2底层基于OkHttp实现的,OkHttp现在已经得到Google官方认可,大量的app都采用OkHttp ...
随机推荐
- Windows Server 2012 Hyper-V 快照
快照 Hyper-V 可提供擷取執行中虛擬機器快照的能力,因此可輕易地回復至前一狀態,對於測試環境相當有幫助. 快照的功用雖然很不錯,不過每次建立快照時都是會消耗相當的硬碟資源,尤其目前的快照點和上一 ...
- U68464 滑稽树上滑稽果(guo)
U68464 滑稽树上滑稽果(guo) 题目描述 小小迪有 n 个约会对象,每个对象有一个约会时长 p[i],小小迪 想尽可能多的去完成他的约会(假设小小迪可以瞬移),每个对象还有 一个忍耐时间 q[ ...
- poj-2752(拓展kmp)
题意:求一个串所有的前后缀字串: 解题思路:kmp和拓展kmp都行,个人感觉拓展kmp更裸一点: 拓展kmp: #include<iostream> #include<algorit ...
- java Builder模式创建不可变类
package com.geostar.gfstack.operationcenter.logger.manager.common; /** * Created by Nihaorz on 2017/ ...
- ajax 提交数组 泛型集合(嵌套集合)
直接上代码 后台接口: A类型中嵌套了 List<B> B类型中嵌套了 List<C> [HttpPost] public string Post(A a) { return ...
- 为什么Elasticsearch查询变得这么慢了?
参考内容:https://mp.weixin.qq.com/s/RTpBaFpNELQCO6VE0KMfsw
- python列表解析式,字典解析式,集合解析式和生成器
一.列表解析式(列表推倒式): 功能:是提供一种方便的列表创建方法,所以,列表解析式返回的是一个列表. 1 lst = [1, 3, 5, 8, 10] 2 ll = [x+x for x in ls ...
- python3,打印一年的某一天是一年的第几天
year = int(input('year:')) month = int(input('month:')) day = int(input('day:')) months = (0,31,59,9 ...
- 阿里云.log
申请证书审核失败的原因及处理方法;( 新添加站点 免费版 SSL 网页内不能有 HTTPS的连接:更多点击连接)
- MobaXterm之取消自动关闭连接 Network error :Connection timed out
连一会就出现连接重来的问题 配置如下.