Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Disable X-Requested-With Header In WebView

I'm trying to make my WebView headers look like the user is just using the regular browser and not using a WebView. From what I can gather the headers are identical apart that the WebView also sends an X-Requested-With header containing the apps package name. Is there any way of preventing this?

like image 703
KingFu Avatar asked Jul 24 '13 11:07

KingFu


People also ask

What is X requested with header?

The X-Requested-With is a request header that a user agent may use to store information about the creation of the request such as client information, method used. Note that the X-Requested-With cannot be added to a cross domain request without the consent of the server via CORS.

How do you override a WebView?

If you want to override certain methods, you have to create a custom WebView class which extends WebView . Also, when you are inflating the WebView , make sure you are casting it to the correct type which is CustomWebView . CustomWebView webView = (CustomWebView) findViewById(R. id.

What can I use instead of WebView?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.

Is Android WebView deprecated?

This interface was deprecated in API level 12. This interface is now obsolete.


1 Answers

You can do it for Android API > 11

public class AndroidMobileAppSampleActivity extends Activity {
Map<String, String> extraHeaders = new HashMap<String, String>();
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
    // must define X-Requested-With, if header missing, then webview will
    //add your package name
    extraHeaders.put("X-Requested-With", "your presentation");
    WebSettings webSettings = mainWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mainWebView.setWebViewClient(new MyCustomWebViewClient());
    mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    mainWebView.loadUrl("http://www.somesite.com", extraHeaders);
}

private class MyCustomWebViewClient extends WebViewClient {
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view,
         String url) {
        // TODO Here you must overwrite request  using your 
        // HttpClient Request
        // and pass it to new WebResourceResponse
        return new  WebResourceResponse(response.ContentType, response.ContentEncoding, responseStream);
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // Apply again your heades here 
        view.loadUrl(url, extraHeaders);
        return true;
    }
}
}
like image 53
anopid Avatar answered Sep 17 '22 17:09

anopid