集册 Java实例教程 从用户代理获取浏览器

从用户代理获取浏览器

欢马劈雪     最近更新时间:2020-01-02 10:19:05

557
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
从用户代理获取浏览器


//package com.nowjava;


public class Main {/*时 代 J a v a 公 众 号 提 供*/

    public static void main(String[] argv) throws Exception {

        String userAgent = "nowjava.com";

        System.out.println(getBrowser(userAgent));

    }


    public static String getBrowser(String userAgent) {

        String browser = "";

        String version = "";

        Integer startLen = 0;

        Integer endLen = 0;

        if (userAgent.toLowerCase().indexOf("msie") != -1) {

            browser = "IE";

            startLen = userAgent.toLowerCase().indexOf("msie");

            endLen = userAgent.indexOf(";", startLen);

            version = userAgent.substring(startLen + 5, endLen);

        } else if (userAgent.toLowerCase().indexOf("trident/7") != -1) {/** 来 自 N o w J a v a . c o m - 时  代  Java**/

            browser = "IE";

            startLen = userAgent.toLowerCase().indexOf("rv:") + 3;

            endLen = userAgent.indexOf(")", startLen);

            version = userAgent.substring(startLen, endLen);

        } else if (userAgent.toLowerCase().indexOf("chrome") != -1) {

            browser = "CHROME";

            startLen = userAgent.toLowerCase().indexOf("chrome") + 7;

            endLen = userAgent.indexOf(" ", startLen);

            version = userAgent.substring(startLen, endLen);

        } else if (userAgent.toLowerCase().indexOf("firefox") != -1) {

            browser = "FIREFOX";

            startLen = userAgent.toLowerCase().indexOf("firefox") + 8;

            endLen = userAgent.length();

            version = userAgent.substring(startLen, endLen);


        } else if (userAgent.toLowerCase().indexOf("safari") != -1) {

            browser = "SAFARI";

            startLen = userAgent.toLowerCase().indexOf("version") + 8;

            endLen = userAgent.indexOf(" ", startLen);

            version = userAgent.substring(startLen, endLen);

        } else if (user
展开阅读全文