0
点赞
收藏
分享

微信扫一扫

利用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

举报

相关推荐

0 条评论