1

I want to send custom headers to server from webView.

My solution works fine for the first page loaded but doesn't work if user click on link in webView.

Following the code.

I extended WebView with my owner class and overrided some methods:

public class SMWebView extends WebView {

private final static  HashMap<String,String> httpHeaders = new HashMap<>();

...

   @Override
   public void loadUrl(String url) {
      super.loadUrl(url,httpHeaders);
   }

   @Override
   public void loadUrl(String url, Map<String, String> additionalHttpHeaders) {
       if(additionalHttpHeaders!=null) {
          additionalHttpHeaders.putAll(httpHeaders);
          super.loadUrl(url, additionalHttpHeaders);
       }
       else{
          super.loadUrl(url, httpHeaders);
       }
    }

    public HashMap<String,String> getHttpHeaders(){
       return httpHeaders;
    }
}

I extended also WebViewClient

public class SMWebViewClient extends WebViewClient {

   @Override
   public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
         android.util.Log.i("MyWebViewClient", "attempting to load resource: " + request.getUrl());

        if(view.getClass().equals(SMWebView.class)){
           SMWebView smWebView = (SMWebView) view;
           request.getRequestHeaders().putAll(smWebView.getHttpHeaders());
           request.getRequestHeaders().put("HIDE_MENU","true");
       }

       return super.shouldInterceptRequest(view,request);
   }
}

And I use both in MainActivity:

public class MainActivity extends AppCompatActivity implements DrawerMenuItem.DrawerCallBack{

   @BindView(R.id.main_webview) SMWebView webView;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       ...
       webView.setWebViewClient(new SMWebViewClient());
       webView.clearCache(true);
       webView.clearHistory();
       webView.getSettings().setJavaScriptEnabled(true);
       webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
       webView.loadUrl(Constant.baseUrl + Constant.homeUrl);
    }
    ...
 }

How can I send my custom headers to each reqeust even I user click on a link on webView?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
fciri
  • 305
  • 7
  • 20
  • 1
    Maybe I found the reason that is explained in this post https://stackoverflow.com/questions/38412965 – fciri Nov 06 '17 at 08:54
  • 1
    Yes, unfortunately it doesn't help to call `super.shouldInterceptRequest()`. You can open a HTTPS connection from your `shouldInterceptRequest()`, though, and add whatever headers you need. Then, you must return (Kotlin code here, for brevity): `WebResourceResponse(conn.contentType.substringBefore(";"), conn.contentType.substringAfter("charset=", "UTF-8"), conn.inputStream)`. See my answer to a related question here: *[Send Post request along with HttpHeaders on Android](https://stackoverflow.com/a/66357697/192373)* – Alex Cohn Feb 24 '21 at 20:13

1 Answers1

0

I solved adding in WebViewClient this methods:

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

@Override
public boolean shouldOverrideUrlLoading(WebView view, String uri) {
    view.loadUrl(uri);
    return true;
}
fciri
  • 305
  • 7
  • 20