Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何获取cookie? #22

Closed
394090466 opened this issue Jul 18, 2016 · 1 comment
Closed

如何获取cookie? #22

394090466 opened this issue Jul 18, 2016 · 1 comment

Comments

@394090466
Copy link

如何获取cookie?

@yanzhenjie
Copy link
Owner

Cookie是可以获取的。
第一点必须要知道的:首先NoHttp自动维持了Cookie。
第二:获取单个请求的Cookie,在响应的时候response.getHeaders().getCookies()
第三:如果需要向webView共享Cookie,那么就要获取这个url下所有的cookie:

try {
            URI uri = new URI(url);
            if (uri != null) {
                CookieStore cookieStore = ((MaMaCookieManager) NoHttp.getDefaultCookieHandler()).getCookieStore();
                List<HttpCookie> cookies = cookieStore.get(uri);
                CookieManager webCookieManager = CookieManager.getInstance();
                webCookieManager.setAcceptCookie(true);
                for (HttpCookie cookie : cookies) {
                    webCookieManager.setCookie(cookie.getDomain(), cookie.getName() + "=" + cookie.getValue() + "; path=" + cookie.getPath() + "; domain=" + cookie.getDomain());
                }
                CookieSyncManager.createInstance(NoHttp.getContext()).sync();
            }
        } catch (URISyntaxException e) {
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants