6

I have a webview activity that loads a URL with a few custom request headers in its onCreate() method. The requirement is to pass the custom headers with the initial URL request. On a few devices, the webview stops sending the headers after the webview activity has been launched a few times.

For example, I have a HomeActivity which launches a WebViewActivity. After launching the WebViewActivity and navigating back to HomeActivity a few times, the WebViewActivity stops sending the custom request headers and this behaviour doesn't change unless I clear the application's data.

I have confirmed this behaviour using a MITM tool. The implementation is as follows:

@Override
protected void onCreate(Bundle savedInstanceState) {

    Map<String, String> map = new HashMap<>();
    map.put("header1", "header1_value");
    map.put("header2", "header2_value");
    map.put("header3", "header3_value");
    map.put("header4", "header4_value");
    webView.loadUrl("https://www.example.com/mypath", map);

}

The above snippet executes unconditionally on every activity launch. However, the headers are not present in the actual request made by the webview. Also, the page being requested is a 303 redirect.

1 Answers1

0

If your minimum API target is level 21, you can use the shouldInterceptRequest else you can use this

With each interception, you will need to take the url, make this request yourself, and return the content stream:

Then:

WebViewClient wvc = new WebViewClient() {
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {

        try {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            httpGet.setHeader("header1", "header1_value");
            httpGet.setHeader("header2", "header2_value");
            httpGet.setHeader("header3", "header3_value");
            httpGet.setHeader("header4", "header4_value");
            HttpResponse httpReponse = client.execute(httpGet);

            Header contentType = httpReponse.getEntity().getContentType();
            Header encoding = httpReponse.getEntity().getContentEncoding();
            InputStream responseInputStream = httpReponse.getEntity().getContent();

            String contentTypeValue = null;
            String encodingValue = null;
            if (contentType != null) {
                contentTypeValue = contentType.getValue();
            }
            if (encoding != null) {
                encodingValue = encoding.getValue();
            }
            return new WebResourceResponse(contentTypeValue, encodingValue, responseInputStream);
        } catch (ClientProtocolException e) {
            //return null to tell WebView we failed to fetch it WebView should try again.
            return null;
        } catch (IOException e) {
             //return null to tell WebView we failed to fetch it WebView should try again.
            return null;
        }
    }
}

//Where wv is your webview
wv.setWebViewClient(wvc);

Based on this question