Android – Webview only applying headers to initial request

I’m writing an android app that uses webview to request content from a web server, but using mWebView.loadUrl(url1, headers); will only apply the headers to the initial request and not the resources in the request.

Any idea as so how to apply the headers to the resource requests as well?

  • Genymotion Android emulator - adb access?
  • Under What Conditions are In-App Billing Version 3 Server Changes Made Available on Client Devices?
  • How to set a ripple effect on textview or imageview on Android?
  • How to power off an Android device?
  • What is orientation constants for views?
  • Google Play In-App Purchase returns error code -1008: null puchaseData or dataSignature
  • Related posts:

    Android Actionbar Sherlock with Tabs
    Android: layout animation like Inshorts news app
    String.Format (.NET) equivalent in Java?
    Android Exif data in JPEG file
    “take a picture and present it” in portrait mode on Samsung Galaxy S
    Are there memory management tips to take care of when making Android Apps?
  • android aapt dump resources no output
  • android takePersistableUriPermission doesn't compile
  • how to sign an already compiled APK
  • Dead code warning?
  • Android - edittext - no textselection handles are shown and no sharing option
  • Using Android:Digits attribute to restrict characters stops action next button working
  • 2 Solutions collect form web for “Android – Webview only applying headers to initial request”

    Not absolutely sure but you can try to override shouldOverrideUrlLoading(WebView view, String url) method and handle all redirects by starting mWebView.loadUrl(url, yourHeaders);
    Dont forget to return true in that overriden method.

    First of all, let me say that i can’t believe that webview sucks so much.

    This is what i did to pass custom headers

    public class CustomWebview extends WebView {
    
    
    
        public void loadWithHeaders(String url) {
    
            setWebViewClient(new WebViewClient() {
    
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
                //makes a custom http request, which allows you to add your own headers
                return customRequest(url);
            }
          });
    
            loadUrl(url);
        }
    
    
        /**
        * Custom http request with headers
        * @param url
        * @return
        */
        private WebResourceResponse customRequest(String url) {
    
        try {
    
            OkHttpClient httpClient = new OkHttpClient();
    
            Request request = new Request.Builder()
                    .url(url.trim())
                    .addHeader("Header-Name",  "Android Sucks")
                    .build();
    
            Response response = httpClient.newCall(request).execute();
    
            return new WebResourceResponse(
                    "text/html", // You can set something other as default content-type
                    "utf-8",  // Again, you can set another encoding as default
                    response.body().byteStream()
            );
        } catch (IOException e) {
            //return null to tell WebView we failed to fetch it WebView should try again.
            return null;
        }
    }
    

    }

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.