Search in sources :

Example 91 with Cookie

use of com.firenio.codec.http11.Cookie in project okhttp-OkGo by jeasonlzy.

the class MemoryCookieStore method saveCookie.

@Override
public synchronized void saveCookie(HttpUrl url, List<Cookie> cookies) {
    List<Cookie> oldCookies = memoryCookies.get(url.host());
    List<Cookie> needRemove = new ArrayList<>();
    for (Cookie newCookie : cookies) {
        for (Cookie oldCookie : oldCookies) {
            if (newCookie.name().equals(oldCookie.name())) {
                needRemove.add(oldCookie);
            }
        }
    }
    oldCookies.removeAll(needRemove);
    oldCookies.addAll(cookies);
}
Also used : Cookie(okhttp3.Cookie) ArrayList(java.util.ArrayList)

Example 92 with Cookie

use of com.firenio.codec.http11.Cookie in project okhttp-OkGo by jeasonlzy.

the class SPCookieStore method loadCookie.

/**
 * 根据当前url获取所有需要的cookie,只返回没有过期的cookie
 */
@Override
public synchronized List<Cookie> loadCookie(HttpUrl url) {
    List<Cookie> ret = new ArrayList<>();
    if (!cookies.containsKey(url.host()))
        return ret;
    Collection<Cookie> urlCookies = cookies.get(url.host()).values();
    for (Cookie cookie : urlCookies) {
        if (isCookieExpired(cookie)) {
            removeCookie(url, cookie);
        } else {
            ret.add(cookie);
        }
    }
    return ret;
}
Also used : Cookie(okhttp3.Cookie) SerializableCookie(com.lzy.okgo.cookie.SerializableCookie) ArrayList(java.util.ArrayList)

Example 93 with Cookie

use of com.firenio.codec.http11.Cookie in project okhttp-OkGo by jeasonlzy.

the class SerializableCookie method parseCursorToBean.

public static SerializableCookie parseCursorToBean(Cursor cursor) {
    String host = cursor.getString(cursor.getColumnIndex(HOST));
    byte[] cookieBytes = cursor.getBlob(cursor.getColumnIndex(COOKIE));
    Cookie cookie = bytesToCookie(cookieBytes);
    return new SerializableCookie(host, cookie);
}
Also used : Cookie(okhttp3.Cookie)

Example 94 with Cookie

use of com.firenio.codec.http11.Cookie in project okhttp-OkGo by jeasonlzy.

the class DBCookieStore method saveCookie.

@Override
public synchronized void saveCookie(HttpUrl url, Cookie cookie) {
    if (!cookies.containsKey(url.host())) {
        cookies.put(url.host(), new ConcurrentHashMap<String, Cookie>());
    }
    // 当前cookie是否过期
    if (isCookieExpired(cookie)) {
        removeCookie(url, cookie);
    } else {
        // 内存缓存
        cookies.get(url.host()).put(getCookieToken(cookie), cookie);
        // 数据库缓存
        SerializableCookie serializableCookie = new SerializableCookie(url.host(), cookie);
        CookieManager.getInstance().replace(serializableCookie);
    }
}
Also used : Cookie(okhttp3.Cookie) SerializableCookie(com.lzy.okgo.cookie.SerializableCookie) SerializableCookie(com.lzy.okgo.cookie.SerializableCookie)

Example 95 with Cookie

use of com.firenio.codec.http11.Cookie in project okhttp-OkGo by jeasonlzy.

the class CookieActivity method addCookie.

@OnClick(R.id.addCookie)
public void addCookie(View view) {
    HttpUrl httpUrl = HttpUrl.parse(Urls.URL_METHOD);
    Cookie.Builder builder = new Cookie.Builder();
    Cookie cookie = builder.name("myCookieKey1").value("myCookieValue1").domain(httpUrl.host()).build();
    CookieStore cookieStore = OkGo.getInstance().getCookieJar().getCookieStore();
    cookieStore.saveCookie(httpUrl, cookie);
    showToast("详细添加cookie的代码,请看demo的代码");
    // 
    OkGo.<String>post(Urls.URL_TEXT_UPLOAD).tag(// 
    this).execute(new StringDialogCallback(this) {

        @Override
        public void onSuccess(Response<String> response) {
            handleResponse(response);
        }

        @Override
        public void onError(Response<String> response) {
            handleError(response);
        }
    });
}
Also used : Cookie(okhttp3.Cookie) StringDialogCallback(com.lzy.demo.callback.StringDialogCallback) CookieStore(com.lzy.okgo.cookie.store.CookieStore) HttpUrl(okhttp3.HttpUrl) OnClick(butterknife.OnClick)

Aggregations

Cookie (okhttp3.Cookie)102 ArrayList (java.util.ArrayList)31 IOException (java.io.IOException)20 HttpUrl (okhttp3.HttpUrl)18 ByteArrayInputStream (java.io.ByteArrayInputStream)16 ObjectInputStream (java.io.ObjectInputStream)16 SharedPreferences (android.content.SharedPreferences)12 Context (android.content.Context)8 HashMap (java.util.HashMap)8 List (java.util.List)8 Map (java.util.Map)8 Test (org.junit.Test)8 Response (okhttp3.Response)7 SerializableCookie (com.lzy.okgo.cookie.SerializableCookie)6 Request (okhttp3.Request)6 SuppressLint (android.annotation.SuppressLint)4 CookieManager (android.webkit.CookieManager)4 EhCookieStore (com.hippo.ehviewer.client.EhCookieStore)4 OnClick (butterknife.OnClick)3 SerializableCookie (com.franmontiel.persistentcookiejar.persistence.SerializableCookie)3