利用request.getHeader("user-agent")获取客户端浏览器和操作系统信息 分享

Of all the headers sent by the browser, the User-Agent header is probably the most useful because it indicates what kind of browser is making the request. Oddly, both Netscape and Internet Explorer identify themselves as Mozilla, which was the nickname for the early Netscape browser. (There is now a browser called Mozilla, which was born from the code for Netscape 5.)

  In case you're wondering where the name Mozilla came from, Netscape was founded by the folks who wrote the old Mosaic Web browser. Netscape Navigator was intended to be a monstrous version of Mosaic: the Godzilla Mosaic, or Mozilla.

  When Internet Explorer (IE) first came out, it lagged behind Netscape in usage, and gradually added features to become a reasonable alternative by the time IE version 3 came along. By identifying itself as Mozilla-compatible, IE is telling the Web server that it can handle anything Mozilla can.

  If you want to figure out whether the browser is Netscape or IE, only IE sends the MSIE string as part of its User-Agent header. Thus, you can do the following test in your JSP or servlet:

if (request.getHeader("USER-AGENT"). indexOf("MSIE") >= 0)
{
// do Internet Explorer specific stuff here
}
else
{
// do Netscape specific stuff here
}

  You can perform similar tests to detect other browsers, such as Opera.
  研究到这里,其实到底USER-AGENT包含哪些元素,自己还不是太清楚,于是写了下面的代码来查看所有的文件头里都有什么:

  Enumeration temp=request.getHeaderNames();
  while (temp.hasMoreElements()){  
  String paramName = (String) temp.nextElement();
  out.print(paramName+"=");
  out.println(request.getHeader(paramName)+"<br>");
}

  在IE浏览器中得到的是:
Accept=*/*
Accept-Language=zh-cn
Accept-Encoding=gzip, deflate
User-Agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; Alexa Toolbar)
Host=localhost:8080
Connection=Keep-Alive
Cookie=JSESSIONID=1zxeFTZUY1grAeAi

  在Firefox中得到的是:
Host=localhost:8080
User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.7.10) Gecko/20050717 Firefox/1.0.6
Accept=text/xml,application/xml,application/xhtml+xml,text/html;
q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language=zh-cn,zh;q=0.5
Accept-Encoding=gzip,deflate
Accept-Charset=gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive=300
Connection=keep-alive
Cookie=JSESSIONID=3wYp8TfGUnbgfxAi
Cache-Control=max-age=0

  在Opera中得到的是:
User-Agent=Opera/8.01 (Windows NT 5.1; U; zh-cn)
Host=localhost:8080
Accept=text/html, application/xml;q=0.9,
application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1
Accept-Language=zh-cn,en;q=0.9
Accept-Charset=gbk, utf-8, utf-16, iso-8859-1;q=0.6, *;q=0.1
Accept-Encoding=deflate, gzip, x-gzip, identity, *;q=0
Cookie=JSESSIONID=3JsNHSp6bxg_0KAi
Cookie2=$Version=1
Cache-Control=no-cache
Connection=Keep-Alive, TE
TE=deflate, gzip, chunked, identity, trailers

 

 

 

 

转自:http://lukas06.blog.sohu.com/81852243.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值