java 解析http user-agent 資訊,javauser-agent

來源:互聯網
上載者:User

java 解析http user-agent 資訊,javauser-agent

解析http user-agent資訊,使用uasparser-0.6.1.jar和jregex-1.2_01.jar兩個包

 

 1 import cz.mallat.uasparser.OnlineUpdater; 2 import cz.mallat.uasparser.UASparser; 3 import cz.mallat.uasparser.UserAgentInfo; 4  5  6 import java.io.IOException; 7  8 /** 9  * Created by Edward on 2016/7/1.10  */11 public class UserAgentUtil {12 13     static UASparser uasParser = null;14 15     // 初始化uasParser對象16     static {17         try {18             uasParser = new UASparser(OnlineUpdater.getVendoredInputStream());19         } catch (IOException e) {20             e.printStackTrace();21         }22     }23 24     public static void main(String[] args)25     {26         String str = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36";27         System.out.println(str);28         try {29             UserAgentInfo userAgentInfo = UserAgentUtil.uasParser.parse(str);30             System.out.println("作業系統名稱:"+userAgentInfo.getOsFamily());//31             System.out.println("作業系統:"+userAgentInfo.getOsName());//32             System.out.println("瀏覽器名稱:"+userAgentInfo.getUaFamily());//33             System.out.println("瀏覽器版本:"+userAgentInfo.getBrowserVersionInfo());//34             System.out.println("裝置類型:"+userAgentInfo.getDeviceType());35             System.out.println("瀏覽器:"+userAgentInfo.getUaName());36             System.out.println("類型:"+userAgentInfo.getType());37 38         } catch (IOException e) {39             e.printStackTrace();40         }41     }42 }

運行結果:

 

 

User-Agent資訊通過瀏覽器內建工具可查看:

如下使用的是chrome瀏覽器

 

程式中static塊,在初始化的時候被執行,初始化情況如下:

1.當建立某個類的新執行個體時;
2.當調用某個類的靜態方法時;
3.當使用某個類或介面的靜態欄位時;
4.當調用Java API中的某些反射方法時,比如類Class中的方法,或者java.lang.reflect中的類的方法時;
5.當初始化某個子類時;
6.當虛擬機器啟動某個被標明為啟動類的類(main方法);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.