In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to use Java to get operating system and browser information", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Java to get operating system and browser information.
Catalogue
The meaning of User Agent
Browser's UA string
UserAgentUtils.jar
Obtain browser type, operating system type and mobile phone model through (User-Agent)
Give another one:
Summary
In a production environment, we need to turn off the swagger configuration to avoid this dangerous behavior of exposing the interface.
The meaning of User Agent
The Chinese name of User Agent is user agent, or UA for short. It is a special string header that enables the server to identify the operating system and version, CPU type, browser and version, browser rendering engine, browser language, browser plug-in and so on.
Some websites often send different pages to different operating systems and different browsers by judging UA, so some pages may not be displayed properly in a browser, but detection can be bypassed by camouflage UA.
Browser's UA string
Standard format: browser ID (operating system ID; encryption level ID; browser language) rendering engine ID version information
Browser identification
Because many websites ignore the two-digit version number when doing UA detection, it may cause the browser and later versions to receive bad pages, so the browser identification entry is fixed as the browser since browser 10, and the real version information is added at the end of the UA string.
Note: from Baidu encyclopedia
UserAgentUtils.jar
UserAgentUtils.jar is a utility class for UserAgent.
Maven is as follows:
Eu.bitwalker UserAgentUtils 1.20
The java code is as follows:
UserAgent userAgent = UserAgent.parseUserAgentString (request.getHeader ("User-Agent")); Browser browser = userAgent.getBrowser (); OperatingSystem os = userAgent.getOperatingSystem ()
Method
Package com.cyj.controller;import javax.servlet.http.HttpServletRequest;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import eu.bitwalker.useragentutils.Browser;import eu.bitwalker.useragentutils.OperatingSystem;import eu.bitwalker.useragentutils.UserAgent / * @ Description: get the ip controller * @ ClassName: IpController.java * @ author ChenYongJia * @ Date 20:25 on April 20, 2019 * @ Email chen87647213@163.com * / @ RestControllerpublic class IpController {private static final Logger log = LoggerFactory.getLogger (IpController.class) / * * get operating system and browser information * @ param request * @ return * / @ RequestMapping (value= "/ browser", method = RequestMethod.GET) public void getBrowser (HttpServletRequest request) {String ua = request.getHeader ("User-Agent") Log.info ("* *"); log.info ("operating system and browser Information:" + ua); / / convert to UserAgent object UserAgent userAgent = UserAgent.parseUserAgentString (ua); / / get browser information Browser browser = userAgent.getBrowser () Log.info ("browser Information:" + browser); / / get system information OperatingSystem os = userAgent.getOperatingSystem (); log.info ("system Information:" + os); / / system name String system = os.getName (); log.info ("system name:" + system); / / browser name String browserName = browser.getName () Log.info ("browser name:" + browserName); log.info ("*");}}
The results are as follows:
* Firefox *
Operating system and browser information: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0
Browser information: FIREFOX
System Information: WINDOWS_7
System name: Windows 7
Browser name: Firefox
* *
* Google *
Operating system and browser information: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36
Browser information: CHROME
System Information: WINDOWS_7
System name: Windows 7
Browser name: Chrome
* *
* IE*
Operating system and browser information: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Browser information: MOZILLA
System Information: WINDOWS_7
System name: Windows 7
Browser name: Mozilla
* *
Obtain browser type, operating system type and mobile phone model through (User-Agent)
Get the User-Agent in the browser request header
String ua = request.getHeader ("User-Agent")
To get the browser type and operating system type, please see the above java code as follows
Get the mobile phone type:
Package com.cyj.controller;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.servlet.http.HttpServletRequest;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import eu.bitwalker.useragentutils.Browser;import eu.bitwalker.useragentutils.OperatingSystem;import eu.bitwalker.useragentutils.UserAgent / * @ Description: get the ip controller * @ ClassName: IpController.java * @ author ChenYongJia * @ Date 20:25 on April 20, 2019 * @ Email chen87647213@163.com * / @ RestControllerpublic class IpController {private static final Logger log = LoggerFactory.getLogger (IpController.class) / * * get operating system and browser information * @ param request * @ return * / @ RequestMapping (value= "/ browser", method = RequestMethod.GET) public void getBrowser (HttpServletRequest request) {UserAgent userAgent = UserAgent.parseUserAgentString (request.getHeader ("User-Agent")); Browser browser = userAgent.getBrowser (); OperatingSystem os = userAgent.getOperatingSystem () Pattern pattern = Pattern.compile (";\ s? (\\ s?\\ s?)\ s? (Build)? /"); Matcher matcher = pattern.matcher ((CharSequence) userAgent); String model = null; if (matcher.find ()) {model = matcher.group (1). Trim () Log.debug ("parse the model through userAgent:" + model);} give another one: package com.cyj.controller;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod Import org.springframework.web.bind.annotation.RestController;import eu.bitwalker.useragentutils.Browser;import eu.bitwalker.useragentutils.OperatingSystem;import eu.bitwalker.useragentutils.UserAgent;import eu.bitwalker.useragentutils.Version;/** @ Description: get the ip controller * @ ClassName: IpController.java * @ author ChenYongJia * @ Date 20:25 on April 20, 2019 * @ Email chen87647213@163.com * / @ RestControllerpublic class IpController {private static final Logger log = LoggerFactory.getLogger (IpController.class) / * * obtain operating system and browser information * * @ param request * @ return * / @ RequestMapping (value = "/ browser", method = RequestMethod.GET) public void service (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String agentStr = request.getHeader ("user-agent") System.out.println (agentStr); UserAgent agent = UserAgent.parseUserAgentString (agentStr); / / browser Browser browser = agent.getBrowser () System.out.println ("Type:" + browser.getBrowserType () + "\ nname:" + browser.getName () + "\ nVendor:" + browser.getManufacturer () + "\ nProduct Series:" + browser.getGroup () + "\ nengine:" + browser.getRenderingEngine () / / browser version Version version = agent.getBrowserVersion (); System.out.println ("="); System.out.println ("major version:" + version.getMajorVersion () + "\ nminor version:" + version.getMinorVersion () + "\ nfull version:" + version.getVersion () / / operating system System.out.println ("= ="); OperatingSystem os = agent.getOperatingSystem () System.out.println ("name:" + os.getName () + "\ ndevice Type:" + os.getDeviceType () + "\ nProduct Family:" + os.getGroup () + "\ nGenerator:" + os.getManufacturer ());}}
Friends, please test the results by yourself!
At this point, I believe you have a deeper understanding of "how to use Java to obtain operating system and browser information". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.