Change Dir

先知cd——热爱生活是一切艺术的开始

统计

留言簿(18)

积分与排名

“牛”们的博客

各个公司技术

我的链接

淘宝技术

阅读排行榜

评论排行榜

User-Agent(UA)的二三事

User-Agent是浏览器请求头中带有的一个头信息,包含了http请求发起者的信息。http协议在请求头中包含这个域的主要目的是为了统计数据,同时可以达到追踪是否有违背协议的情况出现以及根据特定的用户适配特定的页面的需求。浏览器领域的纷争给了UA更多的发展,目前http://www.user-agents.org/列出的ua已经多达上百条。我们目前最常用的就是各种终端设备(尤其是手机)浏览器提交请求时设置的UA,比如苹果iphone手机的UA可能在访问时就是“IUC(U;iOS 5.0.1;Zh-cn;320*480;)/UCWEB8.1.4.125/42/997”,这个比较文艺,看样子像是iphone用UC的;一个纯粹的果粉可能是“Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8C148”,而android的可能就是“|Mozilla/5.0 (Linux; U; Android 2.2.2; zh-cn; ZTE-U V880 Build/FRF91) UC AppleWebKit/530+ (KHTML, like Gecko) Mobile Safari/530”,symbian可能是“MQQBrowser/2.8 (E71-1;SymbianOS/9.1 Series60/3.0)”。我们可以根据UA统计各种设备的访问量,达到统计的目的,同时,如果不做UA伪装的话,也可以找到各种spider和crwaler。

如何去读一个UA呢?根据RFC2616的描述要求,一个ua字段是这样的形式User-Agent = "User-Agent" ":" 1*( product | comment ),方便起见,一般所product描述是依照其重要性来排序的,先出现的相对重要一点。product的形式就是名字和版本号,中间用‘/’隔开。就wikipedia上的说法,大多数的浏览器目前使用的UA格式如下:Mozilla/[version] ([system and browser information]) [platform] ([platform details]) [extensions]。并在文中举例:Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405

其中

  • Mozilla/5.0: Previously used to indicate compatibility with the Mozilla rendering engine (与Mozilla渲染引擎兼容)
  • (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us): Details of the system in which the browser is running (浏览器运行的系统信息)
  • AppleWebKit/531.21.10: The platform the browser uses (浏览器的平台?我觉得应该说是浏览器引擎,具体说是布局引擎)
  • (KHTML, like Gecko): Browser platform details (浏览器引擎信息)
  • Mobile/7B405: This is used by the browser to indicate specific enhancements that are available directly in the browser or through third parties. An example of this is Microsoft Live Meeting which registers an extension so that the Live Meeting service knows if the software is already installed, which means it can provide a streamlined experience to joining meetings
  • 其中各种引擎说明,可以参看这篇文章

    你可以通过这里来测试你的浏览器的UA,当然如果自己写爬虫的话,也记得做好UA的伪装工作。如果要做一个PC站点配套的wap站点,如果能力足够且时间充裕的话,完全可以重写页面,用HTML5或者是普通XHTML来进行。写好以后,条件好的单独部署单独维护,条件差的,可以统一部署,由应用服务器端(apache有开源的模块)根据UA来进行判断需要显示的页面代码,或者交由框架判断也可以(写过滤器对request进行首回合判断)。

    posted on 2012-03-13 18:27 changedi 阅读(2406) 评论(1)  编辑  收藏 所属分类: 杂谈

    评论

    # re: User-Agent(UA)的二三事 2013-01-14 14:42

    gfweagwer  回复  更多评论   


    只有注册用户登录后才能发表评论。


    网站导航: