在Android的WebView中setUserAgentString对在使用loadURL使用HTTP头没有影响()WebView、Android、setUserAgentString、HTTP

2023-09-08 10:19:46 作者:得不到的都释怀

一直在努力改变在一个Android应用程序的HTTP请求的用户代理字符串。我已经使用Wireshark和仿真器一起进行了测试,已经看出,虽然我设置了用户代理字符串中的web视图,相关的使用loadURL请求不使用此用户代理字符串。相反,我看到的Wireshark捕获的Dalvik用户代理字符串。这里是code抽象。有任何想法吗?还是模拟器不支持此?

  @覆盖公共无效的run(){    断言(上下文!= NULL);    ...    ...    web视图=新的WebView(背景);    ...    字符串defaultUserAgent =betaUAteststring;    //清除每个应用程序缓存等    webView.clearCache(真);    webView.clearHistory();    webView.getSettings()setAppCacheEnabled(假)。    。webView.getSettings()setCacheMode(WebSettings.LOAD_NO_CACHE);    webView.getSettings()setJavaScriptEnabled(真)。    webView.setWebViewClient(新WebViewClient(){        @覆盖        公共无效onPageFinished(的WebView视图,字符串URL){        ....        }        @覆盖        公共无效onPageStarted(的WebView视图,字符串URL,位图图标){        ..        }        @覆盖        公共无效onLoadResource(的WebView视图,字符串URL){        ...        }    });    //开始加载    。webView.getSettings()setUserAgentString(defaultUserAgent);    字符串setUA = webView.getSettings()getUserAgentString()。    //  - >这里记录显示正确的用户代理,所以web视图不接受该值    //但下面的语句不会导致与网页视图用户代理HTTP请求    webView.loadUrl(URL);    //替代无助或者(且不应根据的Javadoc)    //地图<字符串,字符串> headerMap =新的HashMap<字符串,字符串>();    //headerMap.put(\"User-Agent\",\"uaTestInAMap);    //webView.loadUrl(url,headerMap);} 

解决方案

回答我的问题。看来,不论什么原因,仿真器不采取从web视图用户代理字符串。我还没有发现这不过的理由。在code正常工作的真实设备上。

Been trying to change the User-Agent string in the HTTP request of an Android app. I have tested this together with wireshark and the emulator, and have seen that although I set the useragent string in the webview, the associated loadUrl request does not use this user-agent string. Instead I see the Dalvik useragent string in the wireshark capture. Here is the code abstract. Any ideas? Or does the emulator not support this?

@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);
}
Android webview教程一

解决方案

Answering my own question. It appears that the emulator for whatever reason is not taking the user agent string from the webview. I have not found out the reason for this however. The code works fine on a real device.