use of okhttp3.Cookie in project OkHttp3 by MrZhousf.
the class SharedPrefsCookiePersistor method removeAll.
@Override
public void removeAll(Collection<Cookie> cookies) {
SharedPreferences.Editor editor = sharedPreferences.edit();
for (Cookie cookie : cookies) {
editor.remove(createCookieKey(cookie));
}
editor.apply();
}
use of okhttp3.Cookie in project OkHttp3 by MrZhousf.
the class SharedPrefsCookiePersistor method loadAll.
@Override
public List<Cookie> loadAll() {
List<Cookie> cookies = new ArrayList<>(sharedPreferences.getAll().size());
for (Map.Entry<String, ?> entry : sharedPreferences.getAll().entrySet()) {
String serializedCookie = (String) entry.getValue();
Cookie cookie = new SerializableCookie().decode(serializedCookie);
cookies.add(cookie);
}
return cookies;
}
use of okhttp3.Cookie in project smartmodule by carozhu.
the class AddCookiesInterceptor method intercept.
@Override
public Response intercept(Chain chain) throws IOException {
final Request.Builder builder = chain.request().newBuilder();
SharedPreferences sharedPreferences = context.getSharedPreferences("cookie", Context.MODE_PRIVATE);
//RxJava的API
Observable.just(sharedPreferences.getString("cookie", "")).subscribe(new Action1<String>() {
@Override
public void call(String cookie) {
//添加cookie
builder.addHeader("Cookie", cookie);
// builder.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
// builder.addHeader("Accept-Encoding", "gzip, deflate");
// builder.addHeader("Connection", "keep-alive");
// builder.addHeader("Accept", "*/*");
// builder.addHeader("Cookie", "add cookies here");
Log.d("AddCookiesInterceptor", "addHeader ------->:" + cookie);
}
});
return chain.proceed(builder.build());
}
use of okhttp3.Cookie in project smartmodule by carozhu.
the class ReceivedCookiesInterceptor method intercept.
@Override
public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(chain.request());
//这里获取请求返回的cookie
if (!originalResponse.headers("Set-Cookie").isEmpty()) {
final StringBuffer cookieBuffer = new StringBuffer();
//这里用了RxJava的相关API大家可以忽略,用自己逻辑实现即可.大家可以用别的方法保存cookie数据
Observable.from(originalResponse.headers("Set-Cookie")).map(new Func1<String, String>() {
@Override
public String call(String s) {
String[] cookieArray = s.split(";");
return cookieArray[0];
}
}).subscribe(new Action1<String>() {
@Override
public void call(String cookie) {
cookieBuffer.append(cookie).append(";");
}
});
SharedPreferences sharedPreferences = mcontext.getSharedPreferences("cookie", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
String[] sessionArry = cookieBuffer.toString().split("=");
editor.putString("cookie", sessionArry[1]);
editor.commit();
Log.d("ReceivedCookiesIntec", "get cookie ------->:" + sessionArry[1]);
} else {
Log.d("ReceivedCookiesIntec", "headers Set-Cookie.isEmpty()");
}
return originalResponse;
}
use of okhttp3.Cookie in project OkHttp3 by MrZhousf.
the class PersistentCookieJar method loadForRequest.
@Override
public synchronized List<Cookie> loadForRequest(HttpUrl url) {
List<Cookie> removedCookies = new ArrayList<>();
List<Cookie> validCookies = new ArrayList<>();
for (Iterator<Cookie> it = cache.iterator(); it.hasNext(); ) {
Cookie currentCookie = it.next();
if (isCookieExpired(currentCookie)) {
removedCookies.add(currentCookie);
it.remove();
} else if (currentCookie.matches(url)) {
validCookies.add(currentCookie);
}
}
persistor.removeAll(removedCookies);
return validCookies;
}
Aggregations