Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a Referer on an android WebView

Is it possible to set the HTTP referer or HTTP header data generally in a WebView?

I know there is a method loadUrl(String url, Map<String, String> additionalHttpHeaders)

How to send a referer request in a url for a webView

But the headers are only sent for the given URL. I need to set headers for every request that is sent via JavaScript from within the WebView.

I Have try to open the webapplication within mobile application.. Does somebody know a solution?

like image 582
bgs Avatar asked Mar 28 '26 02:03

bgs


1 Answers

You can override WebViewClient's method shouldInterceptRequest and edit the request on-the-fly, here is how it is done on Kotlin:

override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest): WebResourceResponse? {
  val url = request.url.toString()
  request.requestHeaders.putIfAbsent("Referer", "YourReferer")
  return null //Returning null means that the WebView should continue loading
}

You can do the same for shouldOverrideUrlLoading, by editing the request header map and then returning false.

Whether you should override them both, depends on your use case. If you want to send referer header for literally loading anything inside a website, then you should override shouldInterceptRequest. However, if you want to send referer header when loading a new website, then you should override shouldOverrideUrlLoading (For example, when moving from page to page). I believe the first one is what you want.

like image 83
Yurowitz Avatar answered Mar 29 '26 16:03

Yurowitz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!