Android:在WebView中禁用X-Requested-With标头

时间:2013-07-24 11:12:20

标签: android android-webview

我试图让我的WebView标题看起来像用户只是使用常规浏览器而不是使用WebView。从我可以收集的信息来看,除了WebView还发送包含应用程序包名称的X-Requested-With标头之外,标题是相同的。有没有办法阻止这种情况?

1 个答案:

答案 0 :(得分:3)

您可以针对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;
    }
}
}