webView in Android loads H5 binding cookie instance
- 2021-11-30 01:21:25
- OfStack
Introduction:
I recently encountered this situation when I was working on a project:
1. It is necessary to use WebView to realize the login registration of H5.
2. Registration for the competition is realized by H5. In these cases, I need to pass cookie to the server to determine whether the current account was successfully logged in. After consulting some information, it was finally settled.
1. Set cookie for 1 loaded link
private void syncCookie(String url) {
try {
CookieSyncManager.createInstance(mWvSignUp.getContext());// Create 1 A cookie Manager
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();// Remove the previous cookie
cookieManager.removeAllCookie();
StringBuilder sbCookie = new StringBuilder();// Create 1 A splice cookie Container of , Why is it so spliced? Please refer to it 1 Under http Head Cookie Structure of
sbCookie.append(_mApplication.getUserInfo().getSessionID());// Splice sessionId
sbCookie.append(String.format(";domain=%s", ""));
sbCookie.append(String.format(";path=%s", ""));
String cookieValue = sbCookie.toString();
cookieManager.setCookie(url, cookieValue);// For url Settings cookie
CookieSyncManager.getInstance().sync();// Synchronization cookie
} catch (Exception e) {
e.printStackTrace();
}
}
2. Before executing loadurl of webview, execute
// Settings cookie
syncCookie(mUrl);
3. Considerations
Note here that after setting cookie, the following attributes cannot be set, otherwise cookie is invalid (not just these attributes, here is just an example, the best way is to set cookie before executing loadurl)
mWvSignUp.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mWvSignUp.getSettings().setJavaScriptEnabled(true);
mWvSignUp.getSettings().setDatabaseEnabled(true);
mWvSignUp.getSettings().setDomStorageEnabled(true);
4.1 What if some ajax requests need to be brought into cookie?
In the project, because sometimes 1 click event is implemented with ajax request, it is also necessary to judge whether to log in or not. The browser will automatically save cookie and send it to the server, but android will not, so we need to intercept the request and attach cookie.
Below 5.0:
mWvSignUp.setWebViewClient(new WebViewClient() {
/**
* 5.0 The following
* @param view
* @param url
* @return
*/
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
syncCookie(url);
return super.shouldInterceptRequest(view, url);// Will be added cookie Adj. url Pass it to the parent class to continue execution
}
});
Above 5.0:
mWvSignUp.setWebViewClient(new WebViewClient() {
@SuppressLint("NewApi")
@Override
public WebResourceResponse shouldInterceptRequest(WebViewview, WebResourceRequest request) {
String url = request.getUrl().toString();
syncCookie(url);
return super.shouldInterceptRequest(view, url);// Because with 5.0 The return value of the following method is the same as that of the 1 Class, so lazy here directly transfer 4.0 Method generates a request
});
Note: I use the lazy way here. If you are interested, you can set cookie by the following methods
mWvSignUp.setWebViewClient(new WebViewClient() {
@SuppressLint("NewApi")
@Override
public WebResourceResponse shouldInterceptRequest(WebViewview, WebResourceRequest request) {
Map<String,String> requestHead = request.getRequestHeaders();// Get the head
return super.shouldInterceptRequest(view,new WebResourceRequest() { // Override the data in the request, including the header, at which time you can set the cookie Stuff into requestHeader Medium
@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;
}
}
})}
});