Server端判别客户端访问的客户端类型



由于项目需求,简化移动App安装,需要在Server端判别客户端的类型:普通PC,Android,IOS,Tablet.

核心原理:解析Request对象,获取“User-Agent”字段对应的值




private static final String[] KNOWN_MOBILE_USER_AGENT_PREFIXES = new String[] {
			"w3c ", "w3c-", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq",
			"bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco",
			"eric", "hipt", "htc_", "inno", "ipaq", "ipod", "jigs", "kddi", "keji",
			"leno", "lg-c", "lg-d", "lg-g", "lge-", "lg/u", "maui", "maxo", "midp",
			"mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-", "newt", "noki",
			"palm", "pana", "pant", "phil", "play", "port", "prox", "qwap", "sage",
			"sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-",
			"siem", "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-",
			"tosh", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi",
			"wapp", "wapr", "webc", "winw", "winw", "xda ", "xda-" };

	private static final String[] KNOWN_MOBILE_USER_AGENT_KEYWORDS = new String[] {
			"blackberry", "webos", "ipod", "lge vx", "midp", "maemo", "mmp", "mobile",
			"netfront", "hiptop", "nintendo DS", "novarra", "openweb", "opera mobi",
			"opera mini", "palm", "psp", "phone", "smartphone", "symbian", "up.browser",
			"up.link", "wap", "windows ce" };

	private static final String[] KNOWN_TABLET_USER_AGENT_KEYWORDS = new String[] {
			"ipad", "playbook", "hp-tablet", "kindle" };


public Device resolveDevice(HttpServletRequest request) {
		String userAgent = request.getHeader("User-Agent");
		// UserAgent keyword detection of Normal devices
		if (userAgent != null) {
			userAgent = userAgent.toLowerCase();
			for (String keyword : normalUserAgentKeywords) {
				if (userAgent.contains(keyword)) {
					return resolveFallback(request);
				}
			}
		}
		// UserAgent keyword detection of Tablet devices
		if (userAgent != null) {
			userAgent = userAgent.toLowerCase();
			// Android special case
			if (userAgent.contains("android") && !userAgent.contains("mobile")) {
				return resolveWithPlatform(DeviceType.TABLET, DevicePlatform.ANDROID);
			}
			// Apple special case
			if (userAgent.contains("ipad")) {
				return resolveWithPlatform(DeviceType.TABLET, DevicePlatform.IOS);
			}
			// Kindle Fire special case
			if (userAgent.contains("silk") && !userAgent.contains("mobile")) {
				return resolveWithPlatform(DeviceType.TABLET, DevicePlatform.UNKNOWN);
			}
			for (String keyword : tabletUserAgentKeywords) {
				if (userAgent.contains(keyword)) {
					return resolveWithPlatform(DeviceType.TABLET, DevicePlatform.UNKNOWN);
				}
			}
		}
		// UAProf detection
		if (request.getHeader("x-wap-profile") != null || request.getHeader("Profile") != null) {
			if (userAgent != null) {
				// Android special case
				if (userAgent.contains("android")) {
					return resolveWithPlatform(DeviceType.MOBILE, DevicePlatform.ANDROID);
				}
				// Apple special case
				if (userAgent.contains("iphone") || userAgent.contains("ipod") || userAgent.contains("ipad")) {
					return resolveWithPlatform(DeviceType.MOBILE, DevicePlatform.IOS);
				}
			}
			return resolveWithPlatform(DeviceType.MOBILE, DevicePlatform.UNKNOWN);
		}
		// User-Agent prefix detection
		if (userAgent != null && userAgent.length() >= 4) {
			String prefix = userAgent.substring(0, 4).toLowerCase();
			if (mobileUserAgentPrefixes.contains(prefix)) {
				return resolveWithPlatform(DeviceType.MOBILE, DevicePlatform.UNKNOWN);
			}
		}
		// Accept-header based detection
		String accept = request.getHeader("Accept");
		if (accept != null && accept.contains("wap")) {
			return resolveWithPlatform(DeviceType.MOBILE, DevicePlatform.UNKNOWN);
		}
		// UserAgent keyword detection for Mobile devices
		if (userAgent != null) {
			// Android special case
			if (userAgent.contains("android")) {
				return resolveWithPlatform(DeviceType.MOBILE, DevicePlatform.ANDROID);
			}
			// Apple special case
			if (userAgent.contains("iphone") || userAgent.contains("ipod") || userAgent.contains("ipad")) {
				return resolveWithPlatform(DeviceType.MOBILE, DevicePlatform.IOS);
			}
			for (String keyword : mobileUserAgentKeywords) {
				if (userAgent.contains(keyword)) {
					return resolveWithPlatform(DeviceType.MOBILE, DevicePlatform.UNKNOWN);
				}
			}
		}
		// OperaMini special case
		@SuppressWarnings("rawtypes")
		Enumeration headers = request.getHeaderNames();
		while (headers.hasMoreElements()) {
			String header = (String) headers.nextElement();
			if (header.contains("OperaMini")) {
				/*return LiteDevice.MOBILE_INSTANCE;*/
				return resolveWithPlatform(DeviceType.MOBILE, DevicePlatform.UNKNOWN);
			}
		}
		return resolveFallback(request);
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值