Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 403
Bahu Avatar asked Jul 31 '19 07:07

Bahu


2 Answers

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());
like image 101
Alireza Tizfahm Fard Avatar answered Nov 08 '22 05:11

Alireza Tizfahm Fard


    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);
like image 38
Chirag Avatar answered Nov 08 '22 05:11

Chirag