public class HttpRequestDeviceUtils {
/** Wap网关Via头信息中特有的描述信息 */
private static String mobileGateWayHeaders[] = new String[] { "ZXWAP",// 中兴提供的wap网关的via信息,例如:Via=ZXWAP
// GateWayZTE
// Technologies,
"chinamobile.com",// 中国移动的诺基亚wap网关,例如:Via=WTP/1.1
// GDSZ-PB-GW003-WAP07.gd.chinamobile.com (Nokia
// WAP Gateway 4.1 CD1/ECD13_D/4.1.04)
"monternet.com",// 移动梦网的网关,例如:Via=WTP/1.1
// BJBJ-PS-WAP1-GW08.bj1.monternet.com. (Nokia WAP
// Gateway 4.1 CD1/ECD13_E/4.1.05)
"infoX",// 华为提供的wap网关,例如:Via=HTTP/1.1 GDGZ-PS-GW011-WAP2 (infoX-WISG
// Huawei Technologies),或Via=infoX WAP Gateway V300R001
// Huawei Technologies
"XMS 724Solutions HTG",// 国外电信运营商的wap网关,不知道是哪一家
"wap.lizongbo.com",// 自己测试时模拟的头信息
"Bytemobile",// 貌似是一个给移动互联网提供解决方案提高网络运行效率的,例如:Via=1.1 Bytemobile OSN
// WebProxy/5.1
};
/** 电脑上的IE或Firefox浏览器等的User-Agent关键词 */
private static String[] pcHeaders = new String[] { "Windows 98",
"Windows ME", "Windows 2000", "Windows XP", "Windows NT", "Ubuntu" };
/** 手机浏览器的User-Agent里的关键词 */
private static String[] mobileUserAgents = new String[] { "Nokia",// 诺基亚,有山寨机也写这个的,总还算是手机,Mozilla/5.0
// (Nokia5800
// XpressMusic)UC
// AppleWebkit(like
// Gecko)
// Safari/530
"SAMSUNG",// 三星手机
// SAMSUNG-GT-B7722/1.0+SHP/VPP/R5+Dolfin/1.5+Nextreaming+SMM-MMS/1.2.0+profile/MIDP-2.1+configuration/CLDC-1.1
"MIDP-2",// j2me2.0,Mozilla/5.0 (SymbianOS/9.3; U; Series60/3.2
// NokiaE75-1 /110.48.125 Profile/MIDP-2.1
// Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML like
// Gecko) Safari/413
"CLDC1.1",// M600/MIDP2.0/CLDC1.1/Screen-240X320
"SymbianOS",// 塞班系统的,
"MAUI",// MTK山寨机默认ua
"UNTRUSTED/1.0",// 疑似山寨机的ua,基本可以确定还是手机
"Windows CE",// Windows CE,Mozilla/4.0 (compatible; MSIE 6.0;
// Windows CE; IEMobile 7.11)
"iPhone",// iPhone是否也转wap?不管它,先区分出来再说。Mozilla/5.0 (iPhone; U; CPU
// iPhone OS 4_1 like Mac OS X; zh-cn) AppleWebKit/532.9
// (KHTML like Gecko) Mobile/8B117
"iPad",// iPad的ua,Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X;
// zh-cn) AppleWebKit/531.21.10 (KHTML like Gecko)
// Version/4.0.4 Mobile/7B367 Safari/531.21.10
"Android",// Android是否也转wap?Mozilla/5.0 (Linux; U; Android
// 2.1-update1; zh-cn; XT800 Build/TITA_M2_16.22.7)
// AppleWebKit/530.17 (KHTML like Gecko) Version/4.0
// Mobile Safari/530.17
"BlackBerry",// BlackBerry8310/2.7.0.106-4.5.0.182
"UCWEB",// ucweb是否只给wap页面? Nokia5800
// XpressMusic/UCWEB7.5.0.66/50/999
"ucweb",// 小写的ucweb貌似是uc的代理服务器Mozilla/6.0 (compatible; MSIE 6.0;)
// Opera ucweb-squid
"BREW",// 很奇怪的ua,例如:REW-Applet/0x20068888 (BREW/3.1.5.20; DeviceId:
// 40105; Lang: zhcn) ucweb-squid
"J2ME",// 很奇怪的ua,只有J2ME四个字母
"YULONG",// 宇龙手机,YULONG-CoolpadN68/10.14 IPANEL/2.0 CTC/1.0
"YuLong",// 还是宇龙
"COOLPAD",// 宇龙酷派YL-COOLPADS100/08.10.S100 POLARIS/2.9 CTC/1.0
"TIANYU",// 天语手机TIANYU-KTOUCH/V209/MIDP2.0/CLDC1.1/Screen-240X320
"TY-",// 天语,TY-F6229/701116_6215_V0230 JUPITOR/2.2 CTC/1.0
"K-Touch",// 还是天语K-Touch_N2200_CMCC/TBG110022_1223_V0801 MTK/6223
// Release/30.07.2008 Browser/WAP2.0
"Haier",// 海尔手机,Haier-HG-M217_CMCC/3.0 Release/12.1.2007
// Browser/WAP2.0
"DOPOD",// 多普达手机
"Lenovo",// 联想手机,Lenovo-P650WG/S100 LMP/LML Release/2010.02.22
// Profile/MIDP2.0 Configuration/CLDC1.1
"LENOVO",// 联想手机,比如:LENOVO-P780/176A
"HUAQIN",// 华勤手机
"AIGO-",// 爱国者居然也出过手机,AIGO-800C/2.04 TMSS-BROWSER/1.0.0 CTC/1.0
"CTC/1.0",// 含义不明
"CTC/2.0",// 含义不明
"CMCC",// 移动定制手机,K-Touch_N2200_CMCC/TBG110022_1223_V0801 MTK/6223
// Release/30.07.2008 Browser/WAP2.0
"DAXIAN",// 大显手机DAXIAN X180 UP.Browser/6.2.3.2(GUI) MMP/2.0
"MOT-",// 摩托罗拉,MOT-MOTOROKRE6/1.0 LinuxOS/2.4.20 Release/8.4.2006
// Browser/Opera8.00 Profile/MIDP2.0 Configuration/CLDC1.1
// Software/R533_G_11.10.54R
"SonyEricsson",// 索爱手机,SonyEricssonP990i/R100 Mozilla/4.0
// (compatible; MSIE 6.0; Symbian OS; 405) Opera
// 8.65 [zh-CN]
"GIONEE",// 金立手机
"HTC",// HTC手机
"ZTE",// 中兴手机,ZTE-A211/P109A2V1.0.0/WAP2.0 Profile
"HUAWEI",// 华为手机,
"webOS",// palm手机,Mozilla/5.0 (webOS/1.4.5; U; zh-CN)
// AppleWebKit/532.2 (KHTML like Gecko) Version/1.0
// Safari/532.2 Pre/1.0
"GoBrowser",// 3g GoBrowser.User-Agent=Nokia5230/GoBrowser/2.0.290
// Safari
"IEMobile",// Windows CE手机自带浏览器,
"WAP2.0"// 支持wap 2.0的
};
/**
* 根据当前请求的特征,判断该请求是否来自手机终端,主要检测特殊的头信息,以及user-Agent这个header
*
* @param request
* http请求
* @return 如果命中手机特征规则,则返回对应的特征字符串
*/
public static boolean isMobileDevice(HttpServletRequest request) {
boolean pcFlag = false;
boolean mobileFlag = false;
String via = request.getHeader("Via");
String userAgent = request.getHeader("user-agent");
for (int i = 0; via != null && !via.trim().equals("")
&& i < mobileGateWayHeaders.length; i++) {
if (via.contains(mobileGateWayHeaders[i])) {
mobileFlag = true;
break;
}
}
for (int i = 0; !mobileFlag && userAgent != null
&& !userAgent.trim().equals("") && i < mobileUserAgents.length; i++) {
if (userAgent.contains(mobileUserAgents[i])) {
mobileFlag = true;
break;
}
}
for (int i = 0; userAgent != null && !userAgent.trim().equals("")
&& i < pcHeaders.length; i++) {
if (userAgent.contains(pcHeaders[i])) {
pcFlag = true;
}
}
if (mobileFlag == true && mobileFlag != pcFlag) {
return true;
}
return false;
}
}
来自编程爱好者熊浪的分享
String userAgent = request.getHeader("user-agent");
if(userAgent.indexOf("Android") != -1){
//安卓
}else if(userAgent.indexOf("iPhone") != -1 || userAgent.indexOf("iPad") != -1){
//苹果
}else{
//电脑
}
以下是ASP
<%
dim u,b,v
set u=Request.ServerVariables("HTTP_USER_AGENT")
set b=new RegExp
set v=new RegExp
b.Pattern="(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino"
v.Pattern="1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-"
b.IgnoreCase=true
v.IgnoreCase=true
b.Global=true
v.Global=true
if b.test(u) or v.test(Left(u,4)) then response.redirect("改成您要跳转的PC或是手机站连接") end if
%>