一直在尝试更改 Android 应用程序的 HTTP 请求中的用户代理字符串。我已经与wireshark和模拟器一起测试过,并且看到虽然我在webview中设置了useragent字符串,但相关的loadUrl请求并没有使用这个user-agent字符串。相反,我在wireshark 捕获中看到了Dalvik useragent 字符串。这是代码摘要。有任何想法吗?还是模拟器不支持这个?
@Override
public void run() {
assert(context != null);
...
...
webView = new WebView(context);
...
String defaultUserAgent = "betaUAteststring";
// Clear per-application caches etc
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setAppCacheEnabled(false);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
....
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
..
}
@Override
public void onLoadResource(WebView view, String url) {
...
}
});
// Start loading
webView.getSettings().setUserAgentString(defaultUserAgent);
String setUA = webView.getSettings().getUserAgentString();
//--> logging here shows the correct user agent, so the webview does accept the value
// However the following statement does not result in an http request with the webviews user agent
webView.loadUrl(url);
//Alternative doesn't help either (and shouldn't according to javadoc)
//Map<String,String> headerMap = new HashMap<String,String>();
//headerMap.put("User-Agent","uaTestInAMap");
//webView.loadUrl(url, headerMap);
}