19

我试图让我的 WebView 标题看起来像用户只是使用常规浏览器而不是使用 WebView。据我所知,标头是相同的,除了 WebView 还发送一个包含应用程序包名称的 X-Requested-With 标头。有没有办法防止这种情况?

4

1 回答 1

4

您可以为 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;
    }
}
}
于 2015-04-01T16:00:10.807 回答