使用Browscap进行服务器端设备检测

Ensuring that websites can adapt to multiple devices – particularly mobile devices – has become both increasingly important and complex. The variety of ways in which people browse the web having mushr...
摘要由CSDN通过智能技术生成

Ensuring that websites can adapt to multiple devices – particularly mobile devices – has become both increasingly important and complex. The variety of ways in which people browse the web having mushroomed. Much of this has been driven by the rise in the use of mobile devices to access the web, although it also applies to devices such as tablets, Internet TV, and so on.

确保网站可以适应多种设备(尤其是移动设备)已经变得越来越重要和复杂。 人们浏览网络的各种方式如雨后春笋般冒出。 尽管移动设备也适用于平板电脑,互联网电视等设备,但其中很大一部分是由使用移动设备访问网络的兴起推动的。

In recent years, Responsive Web Design has become all the rage as a mechanism for adapting websites and applications to cater to multiple devices by using information client-side to adapt layouts accordingly. In a sense, this is a “one-size-fits-all” approach. However, there’s often a case for providing different versions of the same site or using a slightly different approach.

近年来,作为一种通过使用信息客户端相应地调整布局来适应网站和应用程序以适应多种设备的机制,响应式Web设计已成为一种流行。 从某种意义上说,这是“一刀切”的方法。 但是,通常存在提供同一网站的不同版本或使用略有不同的方法的情况。

An alternative solution to the problem is to use server-side device detection and then take certain actions based on that information. One possibility is to simply forward requests for a mobile site to a different URL. Another possibility is to adapt the layout – or indeed content – programmatically as it’s generated on the server.

解决该问题的另一种方法是使用服务器端设备检测,然后根据该信息采取某些措施。 一种可能性是简单地将对移动站点的请求转发到其他URL。 另一种可能性是,在服务器上生成布局时,以编程方式调整布局,甚至调整内容。

Taking a server-side approach is the basis of this article, which looks in detail at the Browser Capabilities Project, or Browscap for short, to provide the information on which to base these decisions.

采用服务器端方法是本文的基础,本文将详细介绍“浏览器功能项目”(简称Browscap),以提供决策依据。

用户代理字符串简介 (An Introduction to User Agent Strings)

Before we can look at server-side detection, we need to understand the information available to the web application which comes in the form of user agent strings.

在查看服务器端检测之前,我们需要了解Web应用程序可用的信息,这些信息以用户代理字符串的形式出现。

When you make an HTTP request to retrieve a page from a web browser, it includes a piece of information called the user agent string. This provides information about who’s making the request and includes a variety of elements.

当您发出HTTP请求以从Web浏览器检索页面时,它包含一条称为用户代理字符串的信息 。 这提供了有关谁在发出请求的信息,并包括各种元素。

Typically the user agent string specifies what browser is used – it’s how analytics services can determine whether you’re using Chrome, Firefox, Safari, IE, or one of a multitude of other browsers. Furthermore, it can also provide information about the version of the requesting application. This is great for identifying the dwindling numbers of IE6 users, for example.

通常,用户代理字符串指定使用哪种浏览器–分析服务可以通过这种方式确定您使用的是Chrome,Firefox,Safari,IE还是其他多种浏览器之一。 此外,它还可以提供有关请求应用程序版本的信息。 例如,这对于识别IE6用户的数量减少非常有用。

Of course a browser is really just a specific type of application. Remember, HTTP requests aren’t just issued when your web browser accesses a web page or other resource. The request may be from a web crawler, a feed reader (ie: RSS), a validator, a library such as cURL, or one of a potentially infinite number of web service clients.

当然,浏览器实际上只是一种特定类型的应用程序。 请记住,当您的Web浏览器访问网页或其他资源时,不仅发出HTTP请求。 该请求可以来自Web爬网程序,提要阅读器(即RSS),验证器,库(例如cURL)或可能无限多个Web服务客户端之一。

The user agent string is also used to provide information about the operating system. Windows, Linux or Mac? iOS or Android? That, too is qualified with version numbers.

用户代理字符串还用于提供有关操作系统的信息。 Windows,Linux或Mac? iOS或Android? 那也符合版本号。

The user agent string will often also provide information about the physical device being used, whether it is a desktop, laptop, mobile, tablet, etc.

用户代理字符串通常还会提供有关正在使用的物理设备的信息,无论它是台式机,笔记本电脑,移动设备,平板电脑等。

Let’s look at some examples of user agent strings:

让我们看一些用户代理字符串的示例:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0

Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0

– This says that the request has been made by Firefox (version 24, to be specific) from an Intel-based Mac.

–这表示请求是由Firefox(具体来说是版本24)从基于Intel的Mac发出的。

Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25

Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25

– Safari on an iPad (running iOS6)

– iPad上的Safari(运行iOS6)

Opera/9.80 (J2ME/MIDP; Opera Mini/9.80 (S60; SymbOS; Opera Mobi/23.348; U; en) Presto/2.5.25 Version/10.54

Opera/9.80 (J2ME/MIDP; Opera Mini/9.80 (S60; SymbOS; Opera Mobi/23.348; U; en) Presto/2.5.25 Version/10.54

– Version 9 of Opera Mini on a Java-based mobile phone

–基于Java的手机上的Opera Mini版本9

Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30

Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30

– Mobile Safari on an Android phone

– Android手机上的Mobile Safari

You can view hundreds of user agent strings online at useragentstring.com/pages/useragentstring.php.

您可以在useragentstring.com/pages/useragentstring.php上在线查看数百个用户代理字符串。

访问Browscap数据 (Accessing Browscap Data)

Considering the number of variables, the number of possible versions, and of course the ever-expanding range of physical devices being used, you’ll realize that the number of different permutations is quite large and growing every day. The Browser Capabilities Project is an attempt to catalogue these permutations. You can think of it of a database of user agent strings which associates them with some of the information which we can glean from them.

考虑到变量的数量,可能的版本的数量以及当然所使用的物理设备的范围不断扩大,您将意识到,不同排列的数量非常大,并且每天都在增加。 浏览器功能项目试图对这些排列进行分类。 您可以想到一个用户代理字符串数据库,该数据库将它们与我们可以从中收集的一些信息相关联。

In reality it’s not simply a database keyed by user agent string; it’s a series of rules and patterns used to extract relevant pieces of information, cross-referenced against known b

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 您可以在程序中使用com.blueconic.browscap.UserAgentParser库以下方式: 1. 在项目中添加依赖。如果您使用Maven,可以在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>com.blueconic</groupId> <artifactId>browscap</artifactId> <version>3.0.0</version> </dependency> ``` 2. 在程序中引入com.blueconic.browscap.UserAgentParser包。 3. 创建com.blueconic.browscap.UserAgentParser对象。 4. 使用parse方法解析用户代理字符串,返回com.blueconic.browscap.UserAgent对象。 ``` import com.blueconic.browscap.UserAgent; import com.blueconic.browscap.UserAgentParser; UserAgentParser parser = new UserAgentParser(); String userAgentString = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36"; UserAgent userAgent = parser.parse(userAgentString); ``` 然后,您可以使用解析出的UserAgent对象访问浏览器相关信息,如浏览器名称、版本、操作系统等。 ``` String browserName = userAgent.getBrowser(); String browserVersion = userAgent.getVersion(); String operatingSystem = userAgent.getOperatingSystem(); ``` ### 回答2: 使用Java库com.blueconic.browscap.UserAgentParser可以解析用户代理字符串,以获取有关用户代理的各种信息。 首先,你需要导入该库,可以通过在你的项目中添加以下依赖项来实现: ```xml <dependency> <groupId>com.blueconic</groupId> <artifactId>browscap</artifactId> <version>3.0.1</version> </dependency> ``` 接下来,你可以创建一个UserAgentParser对象,该对象将提供用于解析用户代理字符串的方法: ```java UserAgentParser parser = new UserAgentParser(); ``` 接下来,你可以使用该库提供的方法来解析用户代理字符串并获取相关的信息。例如,要获取用户代理的浏览器名称,可以使用以下代码: ```java String userAgentString = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"; UserAgent userAgent = parser.parse(userAgentString); String browser = userAgent.getBrowser(); System.out.println("Browser: " + browser); ``` 同样,你可以获取其他信息,例如操作系统、设备类型、是否为移动设备等等。这些信息可以通过以下代码获取: ```java String os = userAgent.getOperatingSystem(); String deviceType = userAgent.getDeviceType(); boolean isMobile = userAgent.isMobile(); ``` 这只是一个简单的示例,你可以根据需要进一步使用该库。该库还提供了其他功能,例如按条件过滤用户代理字符串等。 使用com.blueconic.browscap.UserAgentParser库,你可以方便地解析用户代理字符串以获取关于用户代理的详细信息,并可以据此进行进一步的处理或分析。 ### 回答3: 使用java库com.blueconic.browscap.UserAgentParser可以解析和分析用户代理字符串,从而获取有关用户设备、操作系统和浏览器的信息。 首先,你需要导入com.blueconic.browscap.UserAgentParser库到你的项目中。可以通过在项目的构建文件中添加依赖项或手动下载并导入jar文件实现。 接下来,创建UserAgentParser对象,可以通过以下方式实现: UserAgentParser parser = new UserAgentParser(); 然后,你可以使用UserAgentParser对象来解析用户代理字符串并获取设备、操作系统和浏览器的信息。例如,你可以通过以下方式获取用户设备相关信息: String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36"; UserAgent agent = parser.parse(userAgent); // 获取设备类型 String deviceType = agent.getDeviceType(); // 获取设备名称 String deviceName = agent.getDeviceName(); // 获取设备制造商 String deviceBrand = agent.getDeviceBrand(); 接下来,你可以使用类似的方法获取操作系统和浏览器的相关信息。例如: // 获取操作系统名称 String osName = agent.getOperatingSystem(); // 获取浏览器名称 String browserName = agent.getBrowser(); 使用以上方法,你可以根据用户代理字符串获取有关用户设备、操作系统和浏览器的信息。 最后,记得在使用完UserAgentParser对象后,调用close()方法释放资源: parser.close(); 总结起来,使用java库com.blueconic.browscap.UserAgentParser可以通过解析用户代理字符串来获取用户设备、操作系统和浏览器的信息,这在开发跟踪用户信息、适配网页或统计分析等场景中非常有用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值