如何检测该网站访问者没有使用任何浏览器

时间:2014-11-13 12:37:34

标签: asp.net browser web-crawler bots

上次我遇到需要检测机器人的情况,我就这样解决了。

public class Utility
{
    public static bool IsCrawlByBot()
    {
        List<string> Crawlers = new List<string>()
        {
            "googlebot","bingbot","yandexbot","ahrefsbot","msnbot","linkedinbot","exabot","compspybot",
            "yesupbot","paperlibot","tweetmemebot","semrushbot","gigabot","voilabot","adsbot-google",
            "botlink","alkalinebot","araybot","undrip bot","borg-bot","boxseabot","yodaobot","admedia bot",
            "ezooms.bot","confuzzledbot","coolbot","internet cruiser robot","yolinkbot","diibot","musobot",
            "dragonbot","elfinbot","wikiobot","twitterbot","contextad bot","hambot","iajabot","news bot",
            "irobot","socialradarbot","ko_yappo_robot","skimbot","psbot","rixbot","seznambot","careerbot",
            "simbot","solbot","mail.ru_bot","spiderbot","blekkobot","bitlybot","techbot","void-bot",
            "vwbot_k","diffbot","friendfeedbot","archive.org_bot","woriobot","crystalsemanticsbot","wepbot",
            "spbot","tweetedtimes bot","mj12bot","who.is bot","psbot","robot","jbot","bbot","bot"
        };

        string ua = HttpContext.Current.Request.UserAgent.ToLower();
        bool iscrawler = Crawlers.Exists(x => ua.Contains(x));
        return iscrawler;
    }
}

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    //if (!Request.Browser.Crawler)
    if (!Utility.IsCrawlByBot())
    {
        string strCountryCookie = BBAreman.CountryCookie.GetCookieValue();
        string strShippingCookie = BBAreman.CountryCookie.GetShippingCookieValue();
        if (Request.Url.ToString().IndexOf(".asmx") == -1)
        {
            if (strCountryCookie.Trim() == "" || strShippingCookie.Trim() == "")
            {
                if (Request.Url.GetLeftPart(UriPartial.Authority).ToString() + "/index.aspx?ShowCountry=true" != HttpContext.Current.Request.Url.ToString())
                {
                    Response.Redirect("~/index.aspx?ShowCountry=true");
                }
            }
        }
    }
}

现在我需要知道我的网站是通过除任何浏览器以外的应用程序访问的情况。我可以开发一个例程,它将返回许多浏览器名称,但问题是我知道只有少数浏览器主要用于PC。如何获得所有流行或不流行的浏览器用户代理名称可以从pc,任何设备或移动设备使用。如果我得到它,那么只有当我的网站被浏览器以外的应用程序访问时,我的工作才会更容易记录报告。

2 个答案:

答案 0 :(得分:1)

您可以检查是否支持html / text。

if(Request.Browser.PreferredRenderingMime == "text/html")
{
    Response.Write("This is an HTML device.");
}

http://msdn.microsoft.com/ru-ru/library/system.web.configuration.httpcapabilitiesbase.preferredrenderingmime%28v=vs.110%29.aspx

答案 1 :(得分:1)

首先是谷歌链接。但它需要java。

http://uadetector.sourceforge.net