Android - extracting cookies after login in webview
You can extract all cookies current url by this way from webview as string:
@Override
public void onPageFinished(WebView view, String url){
String cookies = CookieManager.getInstance().getCookie(url);
Log.d(TAG, "All the cookies in a string:" + cookies);
}
How To Get Cookies From WebView?
Instead of long and boring methods, I think you can get cookies from the WebView directly (for example, after user login) by:
@Override
public void onPageFinished(WebView view, String url){
String myCookies = CookieManager.getInstance().getCookie(url);
}
Pass cookies from native login to webview
K7Ko's answer finally worked for me. But only after I commented the line
cookieManager.removeSessionCookie();
Get cookies from webview with path and expiration date
You need to override the WebView's resource loading in order to have access the the response headers (the Cookies are sent as http headers).
Depending on the version of Android you are supporting you need to override the following two methods of the WebViewClient:
mWebview.setWebViewClient(new WebViewClient() {
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
if (request != null && request.getUrl() != null && request.getMethod().equalsIgnoreCase("get")) {
String scheme = request.getUrl().getScheme().trim();
if (scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https")) {
return executeRequest(request.getUrl().toString());
}
}
return null;
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if (url != null) {
return executeRequest(url);
}
return null;
}
});
You can then retrieve the contents of the url yourself and give that to the WebView (by creating a new WebResourceResponse) or return null and let the WebView handle it (take into consideration that this make another call to the network!)
private WebResourceResponse executeRequest(String url) {
try {
URLConnection connection = new URL(url).openConnection();
String cookie = connection.getHeaderField("Set-Cookie");
if(cookie != null) {
Log.d("Cookie", cookie);
}
return null;
//return new WebResourceResponse(connection.getContentType(), connection.getHeaderField("encoding"), connection.getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
WebView not accepting some cookies
If you want to pass your login users data to a cookie in Webview the do it like this
The other day I had to pass my login object to particular URL
as follow.
WebSettings settings = webViewRoi.getSettings();
settings.setDomStorageEnabled(true);
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(false);
settings.setAppCacheEnabled(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setAllowFileAccessFromFileURLs(true);
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
FormsDTO formsDTO = new FormsDTO();
FormsDTOProfile dtoProfile = new FormsDTOProfile(LoginActivity.loginInfoDTO.getProfile());
formsDTO.setProfile(dtoProfile);
formsDTO.setAuthorized(true);
formsDTO.setToken(LoginActivity.loginInfoDTO.getToken());
String out123 = gson.toJson(formsDTO);
String auth2 = URLEncoder.encode(out123, "UTF-8");
String z = "userInfo=" + auth2; // here userinfo is the key of cookie
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie(url, z);
webViewRoi.setWebChromeClient(new WebChromeClient());
webViewRoi.loadUrl(url);
So if you know the key name of cookie then you can pass your login object through cookie. hope it helps you.
How can i get webview to remember cookies?
You have to use CookieSyncManager
to save it, ex:
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
public void onPageFinished(WebView view, String url) {
CookieSyncManager.getInstance().sync();
}
);
Related Topics
When Should One Use Theme.Appcompat VS Themeoverlay.Appcompat
How to Center Icon and Text in a Android Button with Width Set to "Fill Parent"
Android Oncreate or Onstartcommand for Starting Service
How to Take Camera Capture Without a Preview from a Service or Thread
Adb Server Version (36) Doesn't Match This Client (39); Killing
Gradle Error: Minimum Supported Gradle Version Is 3.3. Current Version Is 3.2
How to Make an Http Request Using Cookies on Flutter
How to Apply Zoom, Drag and Rotation to an Image in Android
Load Dimension Value from Res/Values/Dimension.Xml from Source Code
Switching to Landscape Mode in Android Emulator
Can't Include C++ Headers Like Vector in Android Ndk
Android - Custom UI with Custom Attributes
Android: How to Change Checkbox Size
Android Studio Gradle Dsl Method Not Found: 'Android()' -- Error(17,0)
Android Mediaplayer Throwing "Prepare Failed.: Status=0X1" on 2.1, Works on 2.2