集册 Java实例教程 从用户代理获取Bot名称

从用户代理获取Bot名称

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

492
从用户代理获取机器人名称


import javax.servlet.http.HttpServletRequest;
//时代Java公众号 - nowjava.com 提供

public class Main{


    

    public static String[] getBotName(String userAgent) {

        if (userAgent == null) {

            return null;

        }

        userAgent = userAgent.toLowerCase();

        int pos = 0;

        String res = null;/*n o w j a   v  a . c o m - 时  代  Java*/

        if ((pos = userAgent.indexOf("baiduspider")) > -1) {

            res = "Baidu";

            pos += -1;

        } else if ((pos = userAgent.indexOf("sosospider")) > -1) {

            res = "Soso";

            pos += -1;

        } else if ((pos = userAgent.indexOf("sogou web spider/")) > -1) {

            res = "Sogou";

            pos += 17;

        } else if ((pos = userAgent.indexOf("yodaobot")) > -1) {

            res = "Yodao";

            pos += -1;

        } else if ((pos = userAgent.indexOf("yahoo!+slurp")) > -1) {

            res = "Yahoo";

            pos += -1;

        } else if ((pos = userAgent.indexOf("msnbot/")) > -1) {

            res = "MSNBot";

            pos += 7;

        } else if ((pos = userAgent.indexOf("googlebot/")) > -1) {

            res = "Google";

            pos += 10;

        } else if ((pos = userAgent.indexOf("webcrawler/")) > -1) {

            res = "WebCrawler";

            pos += 11;

        } else if ((pos = userAgent.indexOf("inktomi")) > -1) {

            res = "Inktomi";

            pos = -1;

        } else if ((pos = userAgent.indexOf("teoma")) > -1) {

            res = "Teoma";

            pos = -1;

        } else if ((pos = userAgent.indexOf("alexa.com")) > -1) {

            res = "Alexa";

            pos = -1;

        } else if ((pos = userAgent.indexOf("crawler")) > -1) {

            res = "Crawler";

            pos = -1;

        } else if ((pos = userAgent.indexOf("bot")) > -1) {

            res = "Bot";

            pos = -1;

        }

        if (res == null) {

            return null;

        }

        return getArray(res, res, res + getVersionNumber(userAgent, pos));

    }

    private static String[] getArray(String a, String b, String c) {

        String[] res = new String[3];

        res[0] = a;

        res[1] = b;

        res[2] = c;

        return res;

    }

    private static String getVersionNumber(String userAgent, int position) {

        if (position < 0) {

            return "";

        }

        StringBuffer res = new StringBuffer();

        int status = 0;


        while (position < userAgent.length()) {

            char c = userAgent.charAt(position);

            switch (status) {

            case 0:

                // <SPAN class="codecomment"> No valid digits encountered

                // yet</span>

                if (c == ' ' || c == '/') {

                    break;

                }

                if (c == ';' || c == ')') {

                    return "";

                }

                status = 1;

                // fall through

            case 1:

                // <SPAN class="codecomment"> Version number in progress</span>

                if (c == ';' || c == '/' || c == ')' || c == '('

                        || c == '[') {

                    return res.toStr
展开阅读全文