Introduction:
I encountered this situation recently when working on a project:
1. You need to use WebView to implement a H5 login registration.
2. Registration for the competition is achieved with H5. In these cases, I need to pass the cookie to the server to let it determine whether the current account is logged in successfully. After checking some information, I finally got it done.
1. Set cookies for a loaded link
private void syncCookie(String url) { try { (());//Create a cookie manager CookieManager cookieManager = (); (true); ();// Remove previous cookies (); StringBuilder sbCookie = new StringBuilder();//Create a container that splices cookies. Why is it spliced like this? Please check the structure of the http header cookies (_mApplication.getUserInfo().getSessionID());//Split sessionId ((";domain=%s", "")); ((";path=%s", "")); String cookieValue = (); (url, cookieValue);//Set cookies for url ().sync();//Sync cookies } catch (Exception e) { (); } }
2. Before executing the loadurl of the webview, execute it first
// Set cookiessyncCookie(mUrl);
3. Things to note
It should be noted here that after setting cookies, the following attributes cannot be set, otherwise the cookies are invalid (not just these attributes, here are just examples. The best way is to set cookies before executing loadurl)
().setCacheMode(WebSettings.LOAD_NO_CACHE); ().setJavaScriptEnabled(true); ().setDatabaseEnabled(true); ().setDomStorageEnabled(true);
4. What should I do if some ajax requests need to bring in cookies?
In the project, sometimes some click events are implemented using ajax request, and it is also necessary to determine whether to log in. The browser will automatically save the cookie and send it to the server, but Android will not. At this time, we need to intercept the request and attach the cookie.
Below 5.0:
(new WebViewClient() { /** * Below 5.0 * @param view * @param url * @return */ @Override public WebResourceResponse shouldInterceptRequest(WebView view, String url) { syncCookie(url); return (view, url);//Pass the url with the cookie added to the parent class to continue execution } });
5.0 or above:
(new WebViewClient() { @SuppressLint("NewApi") @Override public WebResourceResponse shouldInterceptRequest(WebViewview, WebResourceRequest request) { String url = ().toString(); syncCookie(url); return (view, url);//Because the return value of the method below 5.0 is the same class, so I am lazy here to directly mobilize the 4.0 method to generate a request });
Note: I have adopted a lazy method here. If you are interested, you can set cookies through the following methods.
(new WebViewClient() { @SuppressLint("NewApi") @Override public WebResourceResponse shouldInterceptRequest(WebViewview, WebResourceRequest request) { Map<String,String> requestHead = ();//Get the head return (view,new WebResourceRequest() { //Rewrite the data in the request, including the header, and you can stuff the cookies into the requestHeader at this time @Override public Uri getUrl() { return null; } @Override public boolean isForMainFrame() { return false; } @Override public boolean hasGesture() { return false; } @Override public String getMethod() { return null; } @Override public Map<String, String>getRequestHeaders() { return null; } } })} });
Summarize:There are still many features of webView that we need to explore. If you have some good suggestions, I hope to submit them to me in the comments. I hope I can give you a reference and I hope you can support me more.