java request.getHeader("user-agent")获取浏览器信息的方法
作者:天才小小布 时间:2022-05-28 01:04:26
一、User Agent的含义
User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等。
一些网站常常通过判断 UA 来给不同的操作系统、不同的浏览器发送不同的页面,因此可能造成某些页面无法在某个浏览器中正常显示,但通过伪装 UA 可以绕过检测。
浏览器的 UA 字串
标准格式为: 浏览器标识 (操作系统标识; 加密等级标识; 浏览器语言) 渲染引擎标识 版本信息
浏览器标识
由于很多网站在进行 UA 检测的时候忽略了两位数版本号,所以可能造成 浏览器及之后版本收到糟糕的页面,因此自 浏览器 10 之后的版本中浏览器标识项固定为 浏览器,在 UA 字串尾部添加真实版本信息。
注:来源自百度百科
当在浏览器发起一个请求后,我们可以查看请求信息。
Request Headers:请求头会包含请求信息。
二:获得浏览器请求头中的User-Agent
(1)请求
String userAgent = request.getHeader("user-agent");
(2)获取
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
三、UserAgentUtils.jar
UserAgentUtils.jar是UserAgent的工具类。
(1)maven
<!-- https://mvnrepository.com/artifact/eu.bitwalker/UserAgentUtils -->
<dependency>
<groupId>eu.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<version>1.20</version>
</dependency>
(2)java代码
UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
Browser browser = userAgent.getBrowser();
OperatingSystem os = userAgent.getOperatingSystem();
参考文章:
java 获取HttpRequest Header的几种方法(必看篇)
来源:https://blog.csdn.net/w410589502/article/details/73163383
标签:java,request.getHeader,浏览器信息
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅述int与string类型转换的两种方法
2023-09-09 08:56:28
Java8 HashMap遍历方式性能探讨
2022-01-31 08:48:11
Unity 百度AI实现Logo商标识别
2023-06-09 17:04:26
![](https://img.aspxhome.com/file/2023/5/84465_0s.jpg)
winform 实现选择文件和选择文件夹对话框的简单实例
2022-02-21 07:05:12
SpringCloud分布式链路跟踪的方法
2023-11-24 23:42:19
![](https://img.aspxhome.com/file/2023/6/59936_0s.png)
一起学JAVA基础之运算符
2023-11-06 23:19:01
![](https://img.aspxhome.com/file/2023/6/73026_0s.png)
IntelliJ IDEA 创建spring boot 的Hello World 项目(图解)
2023-03-10 16:13:45
![](https://img.aspxhome.com/file/2023/3/102133_0s.jpg)
Java Swing JLabel标签的使用方法
2021-09-16 05:48:13
![](https://img.aspxhome.com/file/2023/3/68573_0s.jpg)
eclipse springboot工程打war包方法及再Tomcat中运行的方法
2023-04-14 09:21:40
![](https://img.aspxhome.com/file/2023/9/78349_0s.png)
使用Nexus搭建Maven私服教程的方法步骤
2023-03-05 18:56:58
![](https://img.aspxhome.com/file/2023/3/66203_0s.jpg)
解决eclipse上传svn忽略target文件夹的坑
2023-09-12 04:04:23
![](https://img.aspxhome.com/file/2023/5/58445_0s.jpg)
Java访问WebService返回XML数据的方法
2023-11-10 21:23:09
Java Base64解码错误及解决方法
2023-02-09 03:36:35
![](https://img.aspxhome.com/file/2023/5/66515_0s.jpg)
c#详解datetime使用示例
2021-05-26 22:49:04
![](https://img.aspxhome.com/file/2023/4/100094_0s.jpg)
C# WinForm实现自动更新程序之客户端的示例代码
2022-03-28 03:26:24
![](https://img.aspxhome.com/file/2023/0/89750_0s.png)
springmvc 防止表单重复提交的两种方法
2023-03-27 17:57:18
SpringBoot内置tomcat调优测试优化
2023-04-09 03:01:59
![](https://img.aspxhome.com/file/2023/5/120525_0s.png)
C++11 condition_variable条件变量的用法说明
2021-12-06 23:39:56
![](https://img.aspxhome.com/file/2023/9/117789_0s.png)
Android沉浸式状态栏实现
2022-11-23 12:08:12
![](https://img.aspxhome.com/file/2023/3/101843_0s.png)
Android编程实现获取当前连接wifi名字的方法
2023-11-24 15:41:50