cz.msebera.android.httpclient.client.CookieStore Java Examples

The following examples show how to use cz.msebera.android.httpclient.client.CookieStore. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExtendedHttpClient.java    From Overchan-Android with GNU General Public License v3.0 5 votes vote down vote up
private static HttpClient build(final HttpHost proxy, CookieStore cookieStore) {
    SSLCompatibility.waitIfInstallingAsync();
    return HttpClients.custom().
            setDefaultRequestConfig(getDefaultRequestConfigBuilder(HttpConstants.DEFAULT_HTTP_TIMEOUT).build()).
            setUserAgent(HttpConstants.USER_AGENT_STRING).
            setProxy(proxy).
            setDefaultCookieStore(cookieStore).
            setSSLSocketFactory(ExtendedSSLSocketFactory.getSocketFactory()).
            build();
}
 
Example #2
Source File: CloudflareChecker.java    From Overchan-Android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Проверить рекапчу cloudflare, получить cookie
 * @param exception Cloudflare исключение
 * @param httpClient HTTP клиент
 * @param task отменяемая задача
 * @param challenge challenge рекапчи
 * @param recaptchaAnswer ответ на рекапчу
 * @return полученная cookie или null, если проверка не прошла
 */
public Cookie checkRecaptcha(CloudflareException exception, ExtendedHttpClient httpClient, CancellableTask task, String url) {
    if (!exception.isRecaptcha()) throw new IllegalArgumentException("wrong type of CloudflareException");
    HttpResponseModel responseModel = null;
    try {
        HttpRequestModel rqModel = HttpRequestModel.builder().setGET().setNoRedirect(false).build();
        CookieStore cookieStore = httpClient.getCookieStore();
        removeCookie(cookieStore, exception.getRequiredCookieName());
        responseModel = HttpStreamer.getInstance().getFromUrl(url, rqModel, httpClient, null, task);
        for (int i = 0; i < 3  && responseModel.statusCode == 400; ++i) {
            Logger.d(TAG, "HTTP 400");
            responseModel.release();
            responseModel = HttpStreamer.getInstance().getFromUrl(url, rqModel, httpClient, null, task);
        }
        for (Cookie cookie : cookieStore.getCookies()) {
            if (isClearanceCookie(cookie, url, exception.getRequiredCookieName())) {
                Logger.d(TAG, "Cookie found: " + cookie.getValue());
                return cookie;
            }
        }
        Logger.d(TAG, "Cookie is not found");
    } catch (Exception e) {
        Logger.e(TAG, e);
    } finally {
        if (responseModel != null) {
            responseModel.release();
        }
    }
    return null;
}
 
Example #3
Source File: CloudflareChecker.java    From Overchan-Android with GNU General Public License v3.0 5 votes vote down vote up
static void removeCookie(CookieStore store, String name) {
    boolean flag = false;
    for (Cookie cookie : store.getCookies()) {
        if (cookie.getName().equals(name)) {
            if (cookie instanceof SetCookie) {
                flag = true;
                ((SetCookie) cookie).setExpiryDate(new Date(0));
            } else {
                Logger.e(TAG, "cannot remove cookie (object does not implement SetCookie): " + cookie.toString());
            }
        }
    }
    if (flag) store.clearExpired(new Date());
}
 
Example #4
Source File: CookieStoreHelper.java    From BaiduPanApi-Java with MIT License 5 votes vote down vote up
public static String get(CookieStore cookieStore, String key){
    List<Cookie> cookieList  = cookieStore.getCookies();
    for(Cookie cookie:cookieList){
        if(cookie.getName().equals(key)){
            return cookie.getValue();
        }
    }
    return null;
}
 
Example #5
Source File: ExtendedHttpClient.java    From Overchan-Android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Получить хранилище Cookies данного экземпляра
 */
public CookieStore getCookieStore() {
    return cookieStore;
}
 
Example #6
Source File: InterceptingAntiDDOS.java    From Overchan-Android with GNU General Public License v3.0 4 votes vote down vote up
/** Метод anti-DDOS проверки, все запросы перехватываются из webview в httpclient (для использования с прокси-сервером на API >= 11) */
Cookie check(final CloudflareException exception, final ExtendedHttpClient httpClient, final CancellableTask task, final Activity activity) {
    synchronized (lock) {
        if (processing) return null;
        processing = true;
    }
    processing2 = true;
    currentCookie = null;
    
    final HttpRequestModel rqModel = HttpRequestModel.DEFAULT_GET;
    final CookieStore cookieStore = httpClient.getCookieStore();
    CloudflareChecker.removeCookie(cookieStore, exception.getRequiredCookieName());
    final ViewGroup layout = (ViewGroup)activity.getWindow().getDecorView().getRootView();
    final WebViewClient client = new WebViewClient() {
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
            HttpResponseModel responseModel = null;
            try {
                responseModel = HttpStreamer.getInstance().getFromUrl(url, rqModel, httpClient, null, task);
                for (int i = 0; i < 3  && responseModel.statusCode == 400; ++i) {
                    Logger.d(TAG, "HTTP 400");
                    responseModel.release();
                    responseModel = HttpStreamer.getInstance().getFromUrl(url, rqModel, httpClient, null, task);
                }
                for (Cookie cookie : cookieStore.getCookies()) {
                    if (CloudflareChecker.isClearanceCookie(cookie, url, exception.getRequiredCookieName())) {
                        Logger.d(TAG, "Cookie found: " + cookie.getValue());
                        currentCookie = cookie;
                        processing2 = false;
                        return new WebResourceResponse("text/html", "UTF-8", new ByteArrayInputStream("cookie received".getBytes()));
                    }
                }
                BufOutputStream output = new BufOutputStream();
                IOUtils.copyStream(responseModel.stream, output);
                return new WebResourceResponse(null, null, output.toInputStream());
            } catch (Exception e) {
                Logger.e(TAG, e);
            } finally {
                if (responseModel != null) responseModel.release();
            }
            return new WebResourceResponse("text/html", "UTF-8", new ByteArrayInputStream("something wrong".getBytes()));
        }
    };
    
    activity.runOnUiThread(new Runnable() {
        @SuppressLint("SetJavaScriptEnabled")
        @Override
        public void run() {
            webView = new WebView(activity);
            webView.setVisibility(View.GONE);
            layout.addView(webView);
            webView.setWebViewClient(client);
            webView.getSettings().setUserAgentString(HttpConstants.USER_AGENT_STRING);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl(exception.getCheckUrl());
        }
    });

    long startTime = System.currentTimeMillis();
    while (processing2) {
        long time = System.currentTimeMillis() - startTime;
        if ((task != null && task.isCancelled()) || time > CloudflareChecker.TIMEOUT) {
            processing2 = false;
        }
    }
    
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            try {
                layout.removeView(webView);
                webView.stopLoading();
                webView.clearCache(true);
                webView.destroy();
                webView = null;
            } finally {
                processing = false;
            }
        }
    });
    return currentCookie;
}
 
Example #7
Source File: HttpClientHelper.java    From BaiduPanApi-Java with MIT License 4 votes vote down vote up
public static void dumpCookies(CookieStore cookieStore){
    System.out.println("dump");
    System.out.println(cookieStore.getCookies());
    System.out.println("dump end");
}