Java Parsing user-agent Information _java

Source: Internet
Author: User

Parsing HTTP user-agent information, using Uasparser-0.6.1.jar and Jregex-1.2_01.jar two packages

Import Cz.mallat.uasparser.OnlineUpdater;
Import Cz.mallat.uasparser.UASparser;


Import Cz.mallat.uasparser.UserAgentInfo;

Import java.io.IOException;
 /** * Created by Edward on 2016/7/1.

  * * public class Useragentutil {static Uasparser uasparser = null;
    Initializes the Uasparser object static {try {Uasparser = new Uasparser (Onlineupdater.getvendoredinputstream ());
    catch (IOException e) {e.printstacktrace (); } public static void Main (string[] args) {String str = ' mozilla/5.0 (Windows NT 6.1;
    WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/44.0.2403.130 safari/537.36 ";
    System.out.println (str);
      try {useragentinfo useragentinfo = UserAgentUtil.uasParser.parse (str); SYSTEM.OUT.PRINTLN ("Operating system Name:" +useragentinfo.getosfamily ());//System.out.println ("Operating system:" +useragentinfo.getosname ());//System.out.println ("Browser name:" +useragentinfo.getuafamily ());//System.out.println ("browser version:" +USERAGENTINFO.G Etbrowserversioninfo ());//System.out.println ("Device Type:" +useragentinfo.getdevicetype ());
      System.out.println ("Browser:" +useragentinfo.getuaname ());

    System.out.println ("Type:" +useragentinfo.gettype ());
    catch (IOException e) {e.printstacktrace ();

 }
  }
}

Run Result:

User-agent information can be viewed from the browser with its own tools:

The Chrome browser is used as follows

The static block in the program, which is executed at initialization time, is initialized as follows:

1. When creating a new instance of a class;
2. When a static method of a class is invoked;
3. When using a static field of a class or interface;
4. When invoking certain reflection methods in the Java API, such as methods in class classes, or methods of classes in Java.lang.reflect;
5. When initializing a subclass of the class;
6. When the virtual machine starts a class marked as the Startup class (Main method);

The above is the entire content of this article, I hope you can enjoy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.