美文网首页Android
获取android默认的useragent

获取android默认的useragent

作者: HaRun | 来源:发表于2017-02-15 22:47 被阅读2501次

    兼容性最好的就是第一种,推荐使用,代码量少,简单易用

    <pre>

    方法一:

    String useragent = new WebView(this).getSettings().getUserAgentString();

    方法二:

    private static String getUserAgent() {
        String userAgent = "";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            try {
                userAgent = WebSettings.getDefaultUserAgent(getInstance());
            } catch (Exception e) {
                userAgent = System.getProperty("http.agent");
            }
        } else {
            userAgent = System.getProperty("http.agent");
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0, length = userAgent.length(); i < length; i++) {
            char c = userAgent.charAt(i);
            if (c <= '\u001f' || c >= '\u007f') {
                sb.append(String.format("\\u%04x", (int) c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
    

    </pre>

    相关文章

      网友评论

        本文标题:获取android默认的useragent

        本文链接:https://www.haomeiwen.com/subject/tjlcwttx.html