Okhttp 接口请求返回session一致性问题

最近遇到一个奇怪的问题,后台说我们android端每次请求过来的http请求,他们接收后每次sessionid都会变

然后就各种搜,各种操作,session其实是服务器端的东西,每次请求相当于一个会话,前端没有保存session,所以每次请求的session当然不一样。接下来直接上代码,处理session

第一步保存每次请求的session

public class PersistentCookieStore {

private static final String LOG_TAG = "PersistentCookieStore";

private static final String COOKIE_PREFS = "Cookies_Prefs";

private final Map<String, ConcurrentHashMap<String, Cookie>> cookies;

private final SharedPreferences cookiePrefs;

 

public PersistentCookieStore(Context context) {

cookiePrefs = context.getSharedPreferences(COOKIE_PREFS, 0);

cookies = new HashMap<>();

 

//将持久化的cookies缓存到内存中 即map cookies

Map<String, ?> prefsMap = cookiePrefs.getAll();

for (Map.Entry<String, ? > entry : prefsMap.entrySet()) {

String[] cookieNames = TextUtils.split((String) entry.getValue(), ",");

for (String name : cookieNames) {

String encodedCookie = cookiePrefs.getString(name, null);

if (encodedCookie != null) {

Cookie decodedCookie = decodeCookie(encodedCookie);

if (decodedCookie != null) {

if (!cookies.containsKey(entry.getKey())) {

cookies.put(entry.getKey(), new ConcurrentHashMap<String, Cookie>());

}

cookies.get(entry.getKey()).put(name, decodedCookie);

}

}

}

}

}

 

protected String getCookieToken(Cookie cookie) {

return cookie.name() + "@" + cookie.domain();

}

 

public void add(HttpUrl url, Cookie cookie) {

String name = getCookieToken(cookie);

 

//将cookies缓存到内存中 如果缓存过期 就重置此cookie

if (!cookie.persistent()) {

if (!cookies.containsKey(url.host())) {

cookies.put(url.host(), new ConcurrentHashMap<String, Cookie>());

}

cookies.get(url.host()).put(name, cookie);

} else {

if (cookies.containsKey(url.host())) {

cookies.get(url.host()).remove(name);

}

}

 

//将cookies持久化到本地

SharedPreferences.Editor prefsWriter = cookiePrefs.edit();

prefsWriter.putString(url.host(), TextUtils.join(",", cookies.get(url.host()).keySet()));

prefsWriter.putString(name, encodeCookie(new SerializableOkHttpCookies(cookie)));

prefsWriter.apply();

}

 

public List<Cookie> get(HttpUrl url) {

ArrayList<Cookie> ret = new ArrayList<>();

if (cookies.containsKey(url.host()))

ret.addAll(cookies.get(url.host()).values());

return ret;

}

 

public boolean removeAll() {

SharedPreferences.Editor prefsWriter = cookiePrefs.edit();

prefsWriter.clear();

prefsWriter.apply();

cookies.clear();

return true;

}

 

public boolean remove(HttpUrl url, Cookie cookie) {

String name = getCookieToken(cookie);

 

if (cookies.containsKey(url.host()) && cookies.get(url.host()).containsKey(name)) {

cookies.get(url.host()).remove(name);

 

SharedPreferences.Editor prefsWriter = cookiePrefs.edit();

if (cookiePrefs.contains(name)) {

prefsWriter.remove(name);

}

prefsWriter.putString(url.host(), TextUtils.join(",", cookies.get(url.host()).keySet()));

prefsWriter.apply();

 

return true;

} else {

return false;

}

}

 

public List<Cookie> getCookies() {

ArrayList<Cookie> ret = new ArrayList<>();

for (String key : cookies.keySet())

ret.addAll(cookies.get(key).values());

 

return ret;

}

 

/**

* cookies 序列化成 string

*

* @param cookie 要序列化的cookie

* @return 序列化之后的string

*/

protected String encodeCookie(SerializableOkHttpCookies cookie) {

if (cookie == null)

return null;

ByteArrayOutputStream os = new ByteArrayOutputStream();

try {

ObjectOutputStream outputStream = new ObjectOutputStream(os);

outputStream.writeObject(cookie);

} catch (IOException e) {

Log.d(LOG_TAG, "IOException in encodeCookie", e);

return null;

}

 

return byteArrayToHexString(os.toByteArray());

}

 

/**

* 将字符串反序列化成cookies

*

* @param cookieString cookies string

* @return cookie object

*/

protected Cookie decodeCookie(String cookieString) {

byte[] bytes = hexStringToByteArray(cookieString);

ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);

Cookie cookie = null;

try {

ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);

cookie = ((SerializableOkHttpCookies) objectInputStream.readObject()).getCookies();

} catch (IOException e) {

Log.d(LOG_TAG, "IOException in decodeCookie", e);

} catch (ClassNotFoundException e) {

Log.d(LOG_TAG, "ClassNotFoundException in decodeCookie", e);

}

 

return cookie;

}

 

/**

* 二进制数组转十六进制字符串

*

* @param bytes byte array to be converted

* @return string containing hex values

*/

protected String byteArrayToHexString(byte[] bytes) {

StringBuilder sb = new StringBuilder(bytes.length * 2);

for (byte element : bytes) {

int v = element & 0xff;

if (v < 16) {

sb.append('0');

}

sb.append(Integer.toHexString(v));

}

return sb.toString().toUpperCase(Locale.US);

}

 

/**

* 十六进制字符串转二进制数组

*

* @param hexString string of hex-encoded values

* @return decoded byte array

*/

protected byte[] hexStringToByteArray(String hexString) {

int len = hexString.length();

byte[] data = new byte[len / 2];

for (int i = 0; i < len; i += 2) {

data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i + 1), 16));

}

return data;

}

}

第二步

public class SerializableOkHttpCookies {

private transient final Cookie cookies;

private transient Cookie clientCookies;

 

public SerializableOkHttpCookies(Cookie cookies) {

this.cookies = cookies;

}

 

public Cookie getCookies() {

Cookie bestCookies = cookies;

if (clientCookies != null) {

bestCookies = clientCookies;

}

return bestCookies;

}

 

private void writeObject(ObjectOutputStream out) throws IOException {

out.writeObject(cookies.name());

out.writeObject(cookies.value());

out.writeLong(cookies.expiresAt());

out.writeObject(cookies.domain());

out.writeObject(cookies.path());

out.writeBoolean(cookies.secure());

out.writeBoolean(cookies.httpOnly());

out.writeBoolean(cookies.hostOnly());

out.writeBoolean(cookies.persistent());

}

 

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {

String name = (String) in.readObject();

String value = (String) in.readObject();

long expiresAt = in.readLong();

String domain = (String) in.readObject();

String path = (String) in.readObject();

boolean secure = in.readBoolean();

boolean httpOnly = in.readBoolean();

boolean hostOnly = in.readBoolean();

boolean persistent = in.readBoolean();

Cookie.Builder builder = new Cookie.Builder();

builder = builder.name(name);

builder = builder.value(value);

builder = builder.expiresAt(expiresAt);

builder = hostOnly ? builder.hostOnlyDomain(domain) : builder.domain(domain);

builder = builder.path(path);

builder = secure ?builder.secure() : builder;

builder = httpOnly ?builder.httpOnly() : builder;

clientCookies = builder.build();

}

}

第三步对所有请求做拦截,保存session

public class CookiesManager implements CookieJar {

Context context=null;

private final PersistentCookieStore cookieStore = new PersistentCookieStore(Utils.getApp());

 

@Override

public void saveFromResponse(@NonNull HttpUrl url, @NonNull List<Cookie> cookies) {

if (cookies.size() > 0) {

for (Cookie item : cookies) {

cookieStore.add(url, item);

}

}

}

 

@Override

public List<Cookie> loadForRequest(@NonNull HttpUrl url) {

return cookieStore.get(url);

}

}

第四步在okhttp中添加cookiejar

private static final OkHttpClient client = new OkHttpClient.Builder()
        .addNetworkInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request().newBuilder()
                        .addHeader("Connection", "close").build();
                return chain.proceed(request);
            }
        })
        .connectTimeout(20, TimeUnit.SECONDS)
        .readTimeout(20, TimeUnit.SECONDS)
        .writeTimeout(20, TimeUnit.SECONDS)
        .cookieJar(new CookJar())
        // 忽略https认证
        .sslSocketFactory(getSSLSocketFactory())
        .hostnameVerifier(getHostnameVerifier())
        .build();

ok到此搞定了session不一致问题

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值