3

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?

Community
  • 1
  • 1
bgs
  • 3,061
  • 7
  • 40
  • 58

1 Answers1

0

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.

yuroyami
  • 814
  • 6
  • 24