gpt4 book ai didi

Android:在 WebView 中禁用 X-Requested-With header

转载 作者:可可西里 更新时间:2023-11-01 18:48:07 37 4
gpt4 key购买 nike

我试图让我的 WebView header 看起来像用户只是在使用常规浏览器而不是 WebView。据我所知,除了 WebView 还发送包含应用程序包名称的 X-Requested-With header 之外,这些 header 是相同的。有什么办法可以避免这种情况吗?

最佳答案

你可以为 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;
}
}
}

关于Android:在 WebView 中禁用 X-Requested-With header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17832313/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com