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
What's Java Hybrid - Applet + Application
How to Access Static Resources When Mapping a Global Front Controller Servlet on /*
Clicking the Back Button Twice to Exit an Activity
Error:Execution Failed for Task ':App:Dexdebug'. Com.Android.Ide.Common.Process.Processexception
How to Run a Spring Boot Executable Jar in a Production Environment
Java - Io Bound Thread - 1:1 Threading Model
Is There an Java API Viewer on Command Line Like Man for C and Ri for Ruby
How to Implement Basic Authentication with Glassfish
Multiple Row Selection in Jtable
Platform.Runlater and Task in Javafx
Changing Edittext Bottom Line Color with Appcompat V7
Image Encryption/Decryption Using Aes256 Symmetric Block Ciphers
How to Convert a Part of Java Source File to Kotlin
Understanding Metaspace Line in Jvm Heap Printout
Connect to SQL Server from Linux via Jdbc Using Integratedsecurity (Windows Authentication)
Why Does "While(True)" Without "Thread.Sleep" Cause 100% CPU Usage on Linux But Not on Windows