Java examples for Network:User Agent
get Browser from user agent
//package com.java2s; public class Main { public static void main(String[] argv) throws Exception { String userAgent = "java2s.com"; System.out.println(getBrowser(userAgent)); }/* w w w.j a v a 2 s . co m*/ public static String getBrowser(String userAgent) { String browser = ""; String version = ""; Integer startLen = 0; Integer endLen = 0; if (userAgent.toLowerCase().indexOf("msie") != -1) { browser = "IE"; startLen = userAgent.toLowerCase().indexOf("msie"); endLen = userAgent.indexOf(";", startLen); version = userAgent.substring(startLen + 5, endLen); } else if (userAgent.toLowerCase().indexOf("trident/7") != -1) { browser = "IE"; startLen = userAgent.toLowerCase().indexOf("rv:") + 3; endLen = userAgent.indexOf(")", startLen); version = userAgent.substring(startLen, endLen); } else if (userAgent.toLowerCase().indexOf("chrome") != -1) { browser = "CHROME"; startLen = userAgent.toLowerCase().indexOf("chrome") + 7; endLen = userAgent.indexOf(" ", startLen); version = userAgent.substring(startLen, endLen); } else if (userAgent.toLowerCase().indexOf("firefox") != -1) { browser = "FIREFOX"; startLen = userAgent.toLowerCase().indexOf("firefox") + 8; endLen = userAgent.length(); version = userAgent.substring(startLen, endLen); } else if (userAgent.toLowerCase().indexOf("safari") != -1) { browser = "SAFARI"; startLen = userAgent.toLowerCase().indexOf("version") + 8; endLen = userAgent.indexOf(" ", startLen); version = userAgent.substring(startLen, endLen); } else if (userAgent.toLowerCase().indexOf("opera") != -1) { browser = "OPERA"; startLen = userAgent.toLowerCase().indexOf("opera") + 6; endLen = userAgent.length(); version = userAgent.substring(startLen, endLen); } else { browser = "OTHER"; } return browser + "_" + version; } }