Search in sources :

Example 31 with Cookie

use of com.firenio.codec.http11.Cookie in project EhViewer by seven332.

the class UConfigActivity method onDestroy.

@Override
protected void onDestroy() {
    super.onDestroy();
    webView.destroy();
    // Put cookies back to okhttp cookie store
    CookieManager cookieManager = CookieManager.getInstance();
    String cookiesString = cookieManager.getCookie(url);
    if (cookiesString != null && !cookiesString.isEmpty()) {
        EhCookieStore store = EhApplication.getEhCookieStore(this);
        HttpUrl eUrl = HttpUrl.parse(EhUrl.HOST_E);
        HttpUrl exUrl = HttpUrl.parse(EhUrl.HOST_EX);
        // The cookies saved in the uconfig page should be shared between e and ex
        for (String header : cookiesString.split(";")) {
            Cookie eCookie = Cookie.parse(eUrl, header);
            if (eCookie != null) {
                store.addCookie(longLive(eCookie));
            }
            Cookie exCookie = Cookie.parse(exUrl, header);
            if (exCookie != null) {
                store.addCookie(longLive(exCookie));
            }
        }
    }
}
Also used : Cookie(okhttp3.Cookie) EhCookieStore(com.hippo.ehviewer.client.EhCookieStore) CookieManager(android.webkit.CookieManager) HttpUrl(okhttp3.HttpUrl)

Example 32 with Cookie

use of com.firenio.codec.http11.Cookie in project EhViewer by seven332.

the class CookieRepository method getCookieHeader.

public String getCookieHeader(HttpUrl url) {
    List<Cookie> cookies = getCookies(url);
    StringBuilder cookieHeader = new StringBuilder();
    for (int i = 0, size = cookies.size(); i < size; i++) {
        if (i > 0) {
            cookieHeader.append("; ");
        }
        Cookie cookie = cookies.get(i);
        cookieHeader.append(cookie.name()).append('=').append(cookie.value());
    }
    return cookieHeader.toString();
}
Also used : Cookie(okhttp3.Cookie)

Example 33 with Cookie

use of com.firenio.codec.http11.Cookie in project EhViewer by seven332.

the class CookieSet method get.

/**
 * Get cookies for the url. Fill {@code accepted} and {@code expired}.
 */
public void get(HttpUrl url, List<Cookie> accepted, List<Cookie> expired) {
    long now = System.currentTimeMillis();
    Iterator<Map.Entry<Key, Cookie>> iterator = map.entrySet().iterator();
    while (iterator.hasNext()) {
        Cookie cookie = iterator.next().getValue();
        if (cookie.expiresAt() <= now) {
            iterator.remove();
            expired.add(cookie);
        } else if (cookie.matches(url)) {
            accepted.add(cookie);
        }
    }
}
Also used : Cookie(okhttp3.Cookie)

Example 34 with Cookie

use of com.firenio.codec.http11.Cookie in project EhViewer by seven332.

the class CookieRepositoryTest method equals.

private void equals(List<Cookie> cookies1, List<Cookie> cookies2) {
    assertNotNull(cookies1);
    assertNotNull(cookies2);
    assertEquals(cookies1.size(), cookies2.size());
    for (Cookie cookie : cookies1) {
        assertTrue(cookies2.contains(cookie));
    }
}
Also used : Cookie(okhttp3.Cookie)

Example 35 with Cookie

use of com.firenio.codec.http11.Cookie in project EhViewer by seven332.

the class CookieRepositoryTest method testPersistent.

@Test
public void testPersistent() {
    Context app = RuntimeEnvironment.application;
    HttpUrl urlEh = HttpUrl.parse("http://www.ehviewer.com/");
    Cookie cookieEh1 = new Cookie.Builder().name("user").value("1234567890").domain("ehviewer.com").path("/").build();
    Cookie cookieEh2 = new Cookie.Builder().name("level").value("999").domain("www.ehviewer.com").path("/").expiresAt(System.currentTimeMillis() + 100000).build();
    Cookie cookieEh3 = new Cookie.Builder().name("speed").value("10").domain("www.ehviewer.com").path("/").expiresAt(System.currentTimeMillis() + 100000).build();
    HttpUrl urlNMB = HttpUrl.parse("http://h.nimingban.com/");
    Cookie cookieNMB = new Cookie.Builder().name("hash").value("0987654321").domain("nimingban.com").expiresAt(System.currentTimeMillis() + 100000).path("/").build();
    CookieRepository repository = new CookieRepository(app, "cookie.db");
    repository.saveFromResponse(urlEh, Arrays.asList(cookieEh1, cookieEh2, cookieEh3));
    repository.saveFromResponse(urlNMB, Collections.singletonList(cookieNMB));
    Map<String, CookieSet> map = Reflect.on(repository).field("map").get();
    assertEquals(3, map.size());
    equals(map.get("ehviewer.com"), Collections.singletonList(cookieEh1));
    equals(map.get("www.ehviewer.com"), Arrays.asList(cookieEh2, cookieEh3));
    equals(map.get("nimingban.com"), Collections.singletonList(cookieNMB));
    repository.close();
    repository = new CookieRepository(app, "cookie.db");
    map = Reflect.on(repository).field("map").get();
    assertEquals(2, map.size());
    equals(map.get("www.ehviewer.com"), Arrays.asList(cookieEh2, cookieEh3));
    equals(map.get("nimingban.com"), Collections.singletonList(cookieNMB));
    repository.close();
}
Also used : Context(android.content.Context) Cookie(okhttp3.Cookie) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

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