Add Custom Headers to Webview Resource Requests - Android

How to load URL with headers in WebView?


val map = HashMap<String, String>()
map[AUTO_TOKEN] = autoToken
webClientBinding.webView.settings.userAgentString = userAgent
WebView.setWebContentsDebuggingEnabled(true)
webClientBinding.webView.webViewClient = object : WebViewClient() {
override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest): WebResourceResponse? {
CookieManager.getInstance().removeAllCookies(null)
return super.shouldInterceptRequest(view, request)
}
}
webClientBinding.webView.loadUrl(url, map)

It should work!

Android WebView custom headers

No, that is not possible with Android WebView itself. You have to work around either in your page code, or in your app's code, or on the server.

For fixing this on the page's side, you can use XMLHttpRequest for loading subresources. But for that you will have basically to construct the page on the fly.

On the app's side, you can use WebViewClient.shouldInterceptRequest, to intercept all the network requests. You are not allowed to just modify the provided request, instead, you will need to make a new request yourself, but there you will be able to set any headers you want. See this example: Android WebViewClient url redirection (Android URL loading system)

On the server side, you can look into Referer header of subresources, which must contain the url of the page that has requested it.

Android Webview Add header to HTTP request

Hey i have had enough time spent on figuring out this(especially cookies) here is how i solved.

So For your question on how to set header:

1.in Oncreate()

final WebSettings  settings = wv_payment.getSettings();

settings.setJavaScriptEnabled(true);
settings.setDisplayZoomControls(false);
settings.setAppCacheEnabled(true);
settings.setLoadsImagesAutomatically(true);
settings.setBuiltInZoomControls(false);
settings.setPluginState(WebSettings.PluginState.ON);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

The above ones are to enable javascript etc..

To add headers you need to set webViewclient to your webview as follows(here my website needs a basic authentication in header so i'm adding it as follows)

wv_payment.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String credentials = "username" + ":" + "password";

final String basic =
"Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);

HashMap<String, String> headerMap = new HashMap<>();
//put all headers in this header map
headerMap.put("Authorization", basic);


view.loadUrl(url, headerMap);
return true;
}

@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//dismissing loading progress
AppUtils.dismissProgressDialog(progressDialog);
}

@Override
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
//here goes your url authentications if any
handler.proceed("username", "password");
}
});

Comment below if you have any doubts.



Related Topics



Leave a reply



Submit