Bahu
Bahu

Reputation: 1596

How to update request header in WebView

I'm trying to access a URL from android WebView. While loading the URL i inspected through chrome that one header value is null (See the following image).

enter image description here

My question is, i want to update the Origin value. How can i do it?

Note: If Origin has null value only i've to update other wise i've to load as it is

Upvotes: 8

Views: 5272

Answers (4)

Alex Cohn
Alex Cohn

Reputation: 57173

You can create a custom WebViewClient and override the shouldInterceptRequest() method. If you target API 21 and higher, this will be

override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest): WebResourceResponse?

Inside, you will open an HTTPS connection:

val requestUrl = request.url!!.toString()
val httpsUrl = URL(requestUrl)
val conn: HttpsURLConnection = httpsUrl.openConnection() as HttpsURLConnection
for ((key, value) in request.requestHeaders) {
  conn.addRequestProperty(key, value)
}

now you have chance to add new headers or modify the existing ones, e.g.

conn.setRequestProperty("ORIGIN", "https://stackoverflow.com")

when your request is all set, you can wrap the results in a fresh WebResourceResponse:

return WebResourceResponse(
  conn.contentType.substringBefore(";"), 
  conn.contentType.substringAfter("charset=", "UTF-8"), 
  conn.inputStream)

a typical conn.contentType is "text/html; charset=UTF-8". You may add more resilient parsing, or simply use the hardcoded values if you know them.

Upvotes: 1

abhishesh
abhishesh

Reputation: 3316

You can provide additional http headers to webview using

public void loadUrl (String url, Map additionalHttpHeaders)

additionalHttpHeaders Map: the additional headers to be used in the HTTP request for this URL, specified as a map from name to value. Note that if this map contains any of the headers that are set by default by this WebView, such as those controlling caching, accept types or the User-Agent, their values may be overridden by this WebView's defaults.

In your case origin is overridden by web view default http headers.

Upvotes: 0

Chirag
Chirag

Reputation: 156

    webView = (WebView) findViewById(R.id.webView);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);

    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            progressBar.setProgress(progress);
            if (progress == 100) {
                progressBar.setVisibility(View.GONE);
            }
        }
    });




    HashMap<String, String> headers=new HashMap<>();

    webView.loadUrl("https://stackoverflow.com",headers);

Upvotes: 2

Alireza Tizfahmfard
Alireza Tizfahmfard

Reputation: 583

For change request header you can use hashmap and add header key-value pairs

WebView web = findViewById(R.id.webView);

private Map<String, String> getHeaders() {
    Map <String, String> extraHeaders = new HashMap<String, String>();
    extraHeaders.put("Authorization", "Bearer"); 
    return headers;
}

And next step need to create WebViewClient:

private WebViewClient getWebViewClient() {
    return new WebViewClient() {

    @Override
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        view.loadUrl(request.getUrl().toString(), getHeaders());
        return true;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url, getHeaders());
        return true;
    }
};
}

Add WebViewClient to your WebView:

web.setWebViewClient(getWebViewClient());

Upvotes: 3

Related Questions