Search in sources :

Example 61 with Cookie

use of com.firenio.codec.http11.Cookie in project apps-android-wikipedia by wikimedia.

the class SharedPreferenceCookieManager method saveFromResponse.

@Override
public synchronized void saveFromResponse(@NonNull HttpUrl url, @NonNull List<Cookie> cookies) {
    if (cookies.isEmpty()) {
        return;
    }
    boolean cookieJarModified = false;
    for (Cookie cookie : cookies) {
        // Default to the URI's domain if cookie's domain is not explicitly set
        String domainSpec = TextUtils.isEmpty(cookie.domain()) ? url.uri().getAuthority() : cookie.domain();
        if (!cookieJar.containsKey(domainSpec)) {
            cookieJar.put(domainSpec, new ArrayList<>());
        }
        List<Cookie> cookieList = cookieJar.get(domainSpec);
        if (cookie.expiresAt() < System.currentTimeMillis() || "deleted".equals(cookie.value())) {
            Iterator<Cookie> i = cookieList.iterator();
            while (i.hasNext()) {
                if (i.next().name().equals(cookie.name())) {
                    i.remove();
                    cookieJarModified = true;
                }
            }
        } else {
            Iterator<Cookie> i = cookieList.iterator();
            boolean exists = false;
            while (i.hasNext()) {
                Cookie c = i.next();
                if (c.equals(cookie)) {
                    // an identical cookie already exists, so we don't need to update it.
                    exists = true;
                    break;
                } else if (c.name().equals(cookie.name())) {
                    // it's a cookie with the same name, but different contents, so remove the
                    // current cookie, so that the new one will be added.
                    i.remove();
                }
            }
            if (!exists) {
                cookieList.add(cookie);
                cookieJarModified = true;
            }
        }
    }
    if (cookieJarModified) {
        persistCookies();
    }
}
Also used : Cookie(okhttp3.Cookie)

Example 62 with Cookie

use of com.firenio.codec.http11.Cookie in project baseio by generallycloud.

the class TestCookieHeaderServlet method doAccept.

@Override
protected void doAccept(Channel ch, HttpFrame frame) throws Exception {
    String name = frame.getRequestParam("name");
    String value = frame.getRequestParam("value");
    if (Util.isNullOrBlank(name)) {
        name = "test8";
    }
    if (Util.isNullOrBlank(value)) {
        value = Util.randomUUID();
    }
    String cookieLine = frame.getRequestHeader(HttpHeader.Cookie);
    Map<String, String> cookieMap = new HashMap<>();
    if (cookieLine != null) {
        CookieUtil.parseCookies(cookieMap, cookieLine);
    }
    String res = "yes server already accept your message :) " + frame.getRequestParams();
    res += "<BR/>";
    res += cookieMap.toString();
    Cookie c = new Cookie(name, value);
    c.setComment("comment");
    c.setMaxAge(999999);
    frame.setContentType(HttpContentType.text_html_utf8);
    frame.setResponseHeader(HttpHeader.Set_Cookie, c.toString().getBytes());
    frame.setContent(res.getBytes(ch.getCharset()));
    ch.writeAndFlush(frame);
}
Also used : Cookie(com.firenio.codec.http11.Cookie) HashMap(java.util.HashMap)

Example 63 with Cookie

use of com.firenio.codec.http11.Cookie in project DevRing by LJYcoder.

the class PersistentCookieJar method loadForRequest.

@Override
public synchronized List<Cookie> loadForRequest(HttpUrl url) {
    List<Cookie> cookiesToRemove = new ArrayList<>();
    List<Cookie> validCookies = new ArrayList<>();
    for (Iterator<Cookie> it = cache.iterator(); it.hasNext(); ) {
        Cookie currentCookie = it.next();
        if (isCookieExpired(currentCookie)) {
            cookiesToRemove.add(currentCookie);
            it.remove();
        } else if (currentCookie.matches(url)) {
            validCookies.add(currentCookie);
        }
    }
    persistor.removeAll(cookiesToRemove);
    return validCookies;
}
Also used : Cookie(okhttp3.Cookie) ArrayList(java.util.ArrayList)

Example 64 with Cookie

use of com.firenio.codec.http11.Cookie in project DevRing by LJYcoder.

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.commit();
}
Also used : Cookie(okhttp3.Cookie) SharedPreferences(android.content.SharedPreferences)

Example 65 with Cookie

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

the class UConfigActivity method onCreate.

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
    CookieManager cookieManager = CookieManager.getInstance();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        cookieManager.flush();
        cookieManager.removeAllCookies(null);
        cookieManager.removeSessionCookies(null);
    } else {
        CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
        cookieSyncManager.startSync();
        cookieManager.removeAllCookie();
        cookieManager.removeSessionCookie();
        cookieSyncManager.stopSync();
    }
    // Copy cookies from okhttp cookie store to CookieManager
    url = EhUrl.getUConfigUrl();
    EhCookieStore store = EhApplication.getEhCookieStore(this);
    for (Cookie cookie : store.getCookies(HttpUrl.parse(url))) {
        cookieManager.setCookie(url, cookie.toString());
    }
    setContentView(R.layout.activity_u_config);
    setNavigationIcon(R.drawable.v_arrow_left_dark_x24);
    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new UConfigWebViewClient());
    webView.setWebChromeClient(new DialogWebChromeClient(this));
    webView.loadUrl(url);
    progress = (ProgressView) findViewById(R.id.progress);
    Snackbar.make(webView, R.string.apply_tip, Snackbar.LENGTH_LONG).show();
}
Also used : Cookie(okhttp3.Cookie) EhCookieStore(com.hippo.ehviewer.client.EhCookieStore) DialogWebChromeClient(com.hippo.ehviewer.widget.DialogWebChromeClient) CookieSyncManager(android.webkit.CookieSyncManager) CookieManager(android.webkit.CookieManager) SuppressLint(android.annotation.SuppressLint)

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