Search in sources :

Example 51 with Cookie

use of com.firenio.codec.http11.Cookie in project MiMangaNu by raulhaag.

the class RequestWebViewUserAction method onCreate.

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = getIntent().getExtras();
    String web = bundle.getString(WEB_TAG);
    String headers = null, body = null;
    if (bundle.containsKey(HEADERS_TAG)) {
        headers = bundle.getString(HEADERS_TAG);
    }
    if (bundle.containsKey(BODY_TAG)) {
        body = bundle.getString(BODY_TAG);
    }
    setContentView(R.layout.activity_request_webview_user_action);
    WebView wv = findViewById(R.id.webView);
    wv.setWebViewClient(new WebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            RequestWebViewUserAction.this.runOnUiThread(() -> {
                lastCookie = CookieManager.getInstance().getCookie(web);
                onBackPressed();
            });
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if (url.contains("__cf_chl_captcha_tk__")) {
                lastCookie = CookieManager.getInstance().getCookie(web);
                onBackPressed();
            }
        }
    });
    wv.getSettings().setUserAgentString(Navigator.USER_AGENT);
    wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
    wv.getSettings().setDisplayZoomControls(true);
    CookieManager cm = CookieManager.getInstance();
    cm.removeAllCookie();
    HttpUrl url = HttpUrl.parse(web);
    List<Cookie> actCookies = Navigator.getCookieJar().loadForRequest(url);
    cm.setCookie(url.host(), "__cfduid=");
    for (Cookie c : actCookies) {
        cm.setCookie(url.host(), c.name() + "=" + c.value());
    }
    wv.getSettings().setJavaScriptEnabled(true);
    HashMap<String, String> headMap = new HashMap<>();
    headMap.put("Referer", web);
    if (body != null) {
        wv.loadDataWithBaseURL(web, body, null, null, null);
    } else {
        wv.loadUrl(web, headMap);
    }
}
Also used : Cookie(okhttp3.Cookie) HashMap(java.util.HashMap) Bundle(android.os.Bundle) WebView(android.webkit.WebView) CookieManager(android.webkit.CookieManager) HttpUrl(okhttp3.HttpUrl) WebViewClient(android.webkit.WebViewClient) SuppressLint(android.annotation.SuppressLint)

Example 52 with Cookie

use of com.firenio.codec.http11.Cookie in project MiMangaNu by raulhaag.

the class CFInterceptor method addWebViewCookiesToNavigator.

public void addWebViewCookiesToNavigator(String url) {
    cookies = CookieManager.getInstance().getCookie(url);
    String[] cks = cookies.split(";");
    ArrayList<Cookie> cookieArrayList = new ArrayList<>();
    HttpUrl url1 = HttpUrl.parse(url);
    url1 = HttpUrl.parse((url1.isHttps() ? "https://" : "http://") + url1.host());
    for (String c : cks) {
        cookieArrayList.add(Cookie.parse(url1, c + "; Max-Age=36000000; domain=" + removeSubDomains(url1.host())));
    }
    Navigator.getInstance().getHttpClient().cookieJar().saveFromResponse(url1, cookieArrayList);
}
Also used : Cookie(okhttp3.Cookie) ArrayList(java.util.ArrayList) HttpUrl(okhttp3.HttpUrl)

Example 53 with Cookie

use of com.firenio.codec.http11.Cookie in project ForPDA by RadiationX.

the class App method initImageLoader.

public static void initImageLoader(Context context) {
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).imageDownloader(new BaseImageDownloader(context) {

        final Pattern pattern4pda = Pattern.compile("(?:http?s?:)?\\/\\/.*?4pda\\.(?:ru|to)");

        @Override
        public InputStream getStream(String imageUri, Object extra) throws IOException {
            if (imageUri.substring(0, 2).equals("//"))
                imageUri = "http:".concat(imageUri);
            Log.d(App.class.getSimpleName(), "ImageLoader getStream " + imageUri);
            return super.getStream(imageUri, extra);
        }

        @Override
        protected HttpURLConnection createConnection(String url, Object extra) throws IOException {
            HttpURLConnection conn = super.createConnection(url, extra);
            if (pattern4pda.matcher(url).find()) {
                Map<String, Cookie> cookies = App.get().Di().getWebClient().getClientCookies();
                String stringCookies = "";
                for (Map.Entry<String, Cookie> cookieEntry : cookies.entrySet()) {
                    stringCookies = stringCookies.concat(cookieEntry.getKey()).concat("=").concat(cookieEntry.getValue().value()).concat(";");
                }
                conn.setRequestProperty("Cookie", stringCookies);
            }
            return conn;
        }
    }).threadPoolSize(5).threadPriority(Thread.MIN_PRIORITY).denyCacheImageMultipleSizesInMemory().memoryCache(// 5 Mb
    new UsingFreqLimitedMemoryCache(5 * 1024 * 1024)).diskCacheFileNameGenerator(new HashCodeFileNameGenerator()).defaultDisplayImageOptions(options.build()).build();
    ImageLoader.getInstance().init(config);
}
Also used : Cookie(okhttp3.Cookie) Pattern(java.util.regex.Pattern) HashCodeFileNameGenerator(com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator) UsingFreqLimitedMemoryCache(com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache) HttpURLConnection(java.net.HttpURLConnection) BaseImageDownloader(com.nostra13.universalimageloader.core.download.BaseImageDownloader) ImageLoaderConfiguration(com.nostra13.universalimageloader.core.ImageLoaderConfiguration) Map(java.util.Map) HashMap(java.util.HashMap)

Example 54 with Cookie

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

the class PersistentCookieStore method decodeCookie.

/**
     * 将字符串反序列化成cookies
     *
     * @param cookieString cookies string
     * @return cookie object
     */
private Cookie decodeCookie(String cookieString) {
    byte[] bytes = hexStringToByteArray(cookieString);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    Cookie cookie = null;
    try {
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        cookie = ((SerializableHttpCookie) objectInputStream.readObject()).getCookie();
    } catch (IOException e) {
        Log.d(LOG_TAG, "IOException in decodeCookie", e);
    } catch (ClassNotFoundException e) {
        Log.d(LOG_TAG, "ClassNotFoundException in decodeCookie", e);
    }
    return cookie;
}
Also used : Cookie(okhttp3.Cookie) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 55 with Cookie

use of com.firenio.codec.http11.Cookie in project okhttputils by hongyangAndroid.

the class PersistentCookieStore method decodeCookie.

protected Cookie decodeCookie(String cookieString) {
    byte[] bytes = hexStringToByteArray(cookieString);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    Cookie cookie = null;
    try {
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        cookie = ((SerializableHttpCookie) objectInputStream.readObject()).getCookie();
    } catch (IOException e) {
        Log.d(LOG_TAG, "IOException in decodeCookie", e);
    } catch (ClassNotFoundException e) {
        Log.d(LOG_TAG, "ClassNotFoundException in decodeCookie", e);
    }
    return cookie;
}
Also used : Cookie(okhttp3.Cookie) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

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