Capture Requests and Get Cookies of Web Page in a WebView in Android

In this article we will learn how to capture requests and get cookies when a web page is loaded into a WebView in Android.

Using WebView we can create an Android application but not a native Android application.

Assuming we want to customize that application, we must capture the request executed when the web page is loaded.

When the request is captured then we can only redirect the control to an activity then we can develop the activity using an overridden method in WebViewClient.

  1. public void onLoadResource(WebView view, String url){}  
For instance:
  1. WebView mWebView;  
  2. mWebView.setWebViewClient(new MyWebViewClient()  
  3.   
  4. {  
  5.    @Override  
  6.    public void onLoadResource(WebView view, String url){  
  7.   
  8.       System.out.println("Current URL : "+ url + " " + Html.fromHtml("<br>") + " is http url ? "+ URLUtil.isHttpUrl(url));  
  9.   
  10.       if( url.equals("http://www.c-sharpcorner.com/1/247/android-programming.aspx"))  
  11.       {  
  12.          Toast.makeText(getApplicationContext(), "Android Articles page is loading... ", Toast.LENGTH_LONG).show();  
  13.       }   
  14.    } 
  15. }
Assume we want to get cookies to get session details and other information, then depending on the URL we can get the cookies.

We can also get the session of the web page loaded in the WebView.

CookieSyncManager

The CookieSyncManager synchronizes the browser cookie store between main memory and permanent storage. 
  1. CookieSyncManager.getInstance().sync();    
For instance:
  1. public void onPageFinished(WebView view, String url){  
  2.    CookieSyncManager.getInstance().sync();  
  3.    URL url1 = null;  
  4.    try {  
  5.       url1 = new URL(url);  
  6.    } 
  7.    catch (MalformedURLException e) {  
  8.       // TODO Auto-generated catch block   
  9.       e.printStackTrace();  
  10.    }  
  11.    System.out.println("C sharp corner user Ref " + url1.getRef());  
  12.    System.out.println(" C sharp corner user host " + url1.getHost());  
  13.    System.out.println("C sharp corner user authority " + url1.getAuthority());  
  14.    String cookies = CookieManager.getInstance().getCookie(url);  
  15.    System.out.println("All COOKIES " + cookies);  
  16.    Toast.makeText(getApplicationContext(),"All Cookies " + cookies , Toast.LENGTH_LONG).show();  
Source Code

MainActivity.java

  1. package com.example.webviewdemo;  
  2. import java.net.MalformedURLException;  
  3. import java.net.URL;  
  4. import android.annotation.SuppressLint;  
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.os.Bundle;  
  8. import android.text.Html;  
  9. import android.webkit.CookieManager;  
  10. import android.webkit.CookieSyncManager;  
  11. import android.webkit.URLUtil;  
  12. import android.webkit.WebView;  
  13. import android.webkit.WebViewClient;  
  14. import android.widget.Toast;  
  15. public class MainActivity extends Activity {  
  16.     public Context context;  
  17.     public class MyWebViewClient extends WebViewClient {}  
  18.     WebView mWebView;  
  19.     String url = "http://www.c-sharpcorner.com/";@SuppressLint("SetJavaScriptEnabled")@Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.         mWebView = (WebView) findViewById(R.id.webView1);  
  24.         mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);  
  25.         mWebView.getSettings().setBuiltInZoomControls(true);  
  26.         mWebView.setWebViewClient(new MyWebViewClient() {@Override  
  27.             public void onLoadResource(WebView view, String url) {  
  28.                 System.out.println("Current URL : " + url + " " + Html.fromHtml("<br>") + " is http url ? " + URLUtil.isHttpUrl(url));  
  29.                 if (url.equals("http://www.c-sharpcorner.com/1/247/android-programming.aspx")) {  
  30.                     Toast.makeText(getApplicationContext(), "Android Articles page is loading... ", Toast.LENGTH_LONG).show();  
  31.                 }  
  32.             }@Override  
  33.             public void onPageFinished(WebView view, String url) {  
  34.                 CookieSyncManager.getInstance().sync();  
  35.                 URL url1 = null;  
  36.                 try {  
  37.                     url1 = new URL(url);  
  38.                 } catch (MalformedURLException e) {  
  39.                     // TODO Auto-generated catch block     
  40.                     e.printStackTrace();  
  41.                 }  
  42.                 System.out.println("C sharp corner user Ref " + url1.getRef());  
  43.                 System.out.println(" C sharp corner user host " + url1.getHost());  
  44.                 System.out.println("C sharp corner user authority " + url1.getAuthority());  
  45.                 String cookies = CookieManager.getInstance().getCookie(url);  
  46.                 System.out.println("All COOKIES " + cookies);  
  47.                 Toast.makeText(getApplicationContext(), "All Cookies " + cookies, Toast.LENGTH_LONG).show();  
  48.             }  
  49.         });  
  50.         mWebView.getSettings().setJavaScriptEnabled(true);  
  51.         mWebView.loadUrl(url);  
  52.     }@Override  
  53.     public void onBackPressed() {  
  54.         if (mWebView.canGoBack()) {  
  55.             mWebView.goBack();  
  56.         } else {  
  57.             super.onBackPressed();  
  58.         }  
  59.     }  
  60. }  
activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.    android:layout_width="match_parent"  
  4.    android:layout_height="match_parent"  
  5.    android:orientation="vertical" >  
  6.    <WebView  
  7.    android:id="@+id/webView1"  
  8.    android:layout_width="match_parent"  
  9.    android:layout_height="match_parent" />  
  10. </LinearLayout>
Note:

Add Permission of internet in manifest file.
  1. <uses-permission android:name="android.permission.INTERNET" /> 
Output

After capturing the request URL before a web page:


                        Figure 1: Android Program

After getting a cookies session in a WebView:


                                       Figure 2: WebView