Java judges requests from mobile phones or computers

Source: Internet
Author: User
Tags trim lenovo
The code is as follows: Copy code

Package com. zhuzher. commons. util;
 
Import javax. servlet. http. HttpServletRequest;
 
Public class HttpRequestDeviceUtils {
/** Specific descriptions in Wap gateway Via header information */
Private static String mobileGateWayHeaders [] = new String [] {
"ZXWAP", // The via information of the wap gateway provided by ZTE, such as: Via = ZXWAP GateWayZTE Technologies,
"Chinamobile.com", // China Mobile's Nokia wap Gateway, e.g.: Via = WTP/1.1 GDSZ-PB-GW003-WAP07.gd.chinamobile.com (Nokia WAP Gateway 4.1 CD1/ECD13_D/4.1.04)
"Monternet.com", // mobile Dream Network Gateway, for example: Via = WTP/1.1 BJBJ-PS-WAP1-GW08.bj1.monternet.com. (Nokia WAP Gateway 4.1 CD1/ECD13_E/4.1.05)
"InfoX", // wap Gateway provided by Huawei, for example, Via = HTTP/1.1 GDGZ-PS-GW011-WAP2 (infoX-WISG Huawei Technologies), or Via = infoX WAP Gateway V300R001 Huawei Technologies
"XMS 724 Solutions HTG", // The wap gateway of a foreign telecom operator.
"Wap.lizongbo.com", // The simulated header information during the test.
"Bytemobile", // It seems to be a solution provided to the mobile Internet to improve network operation efficiency, for example: Via = 1.1 Bytemobile OSN WebProxy/5.1
};
/** User-Agent keywords such as IE or Firefox on the computer */
Private static String [] pcHeaders = new String [] {
"Windows 98 ",
"Windows ME ",
"Windows 2000 ",
"Windows XP ",
"Windows NT ",
"Ubuntu"
};
/** Keywords in the User-Agent of the mobile browser */
Private static String [] mobileUserAgents = new String [] {
"Nokia", // Nokia, which is also written on a stockade machine, is still a mobile phone, Mozilla/5.0 (Nokia5800 XpressMusic) UC AppleWebkit (like Gecko) Safari/530
"SAMSUNG", // SAMSUNG mobile 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", // Seban system,
"MAUI", // Default ua
"UNTRUSTED/1.0", // It is suspected that the ua of the cottage machine is still a mobile phone.
"Windows CE", // Windows CE, Mozilla/4.0 (compatible; MSIE 6.0; Windows CE; IEMobile 7.11)
"IPhone", // does the iPhone also turn to wap? Regardless of it, distinguish it first. 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", // ua of iPad, 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", // does Android switch to 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", // does ucweb only provide wap pages? Nokia5800 XpressMusic/UCWEB7.5.0.66/50/999
"Ucweb", // The lower-case ucweb looks like the uc proxy server Mozilla/6.0 (compatible; MSIE 6.0;) Opera ucweb-squid
"BREW", // Very strange ua, such as: REW-Applet/0x20068888 (BREW/3.1.5.20; DeviceId: 40105; Lang: zhcn) ucweb-squid
"Corner Stone", // A strange ua, with only four letters
"YULONG", // YULONG phone, YULONG-CoolpadN68/10.14 IPANEL/2.0 CTC/1.0
"YuLong", // or YuLong
"COOLPAD", // Yulong Copi YL-COOLPADS100/08.10.100 POLARIS/2.9 CTC/1.0
"TIANYU", // TIANYU phone TIANYU-KTOUCH/V209/MIDP2.0/CLDC1.1/Screen-240X320
"TY-", // days, TY-F6229/701116_6215_V0230 JUPITOR/2.2 CTC/1.0
"K-Touch", // or Tianyu K-Touch_N2200_CMCC/TBG110022_1223_V0801 MTK/6223 Release/30.07.2008 Browser/WAP2.0
"Haier", // Haier mobile phone, Haier-HG-M217_CMCC/3.0 Release/12.1.2007 Browser/WAP2.0
"DOPOD", // your mobile phone number
"Lenovo", // Lenovo phone, Lenovo-P650WG/100lmp/LML Release/2010.02.22 Profile/MIDP2.0 Configuration/CLDC1.1
"LENOVO", // LENOVO phone, such as: LENOVO-P780/176A
"HUAQIN", // HUAQIN mobile phone
"AIGO-", // Patriot has also been out of the phone, AIGO-800C/2.04 TMSS-BROWSER/1.0.0 CTC/1.0
"CTC/1.0", // Unknown meaning
"CTC/2.0", // Unknown meaning
"CMCC", // mobile phone, 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-", // Motorola, 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", // Sony Ericsson mobile phone, SonyEricssonP990i/R100 Mozilla/4.0 (compatible; MSIE 6.0; Symbian OS; 405) Opera 8.65 [zh-CN]
"GIONEE", // GIONEE mobile phone
"HTC", // HTC mobile phone
"ZTE", // ZTE mobile phone, ZTE-A211/pda-a2v1.0.0/WAP2.0 Profile
"HUAWEI", // HUAWEI mobile phone,
"WebOS", // palm mobile phone, 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 mobile browser,
"WAP2.0" // supports wap 2.0
};
/**
* Based on the characteristics of the current request, determine whether the request comes from a mobile terminal. It mainly checks the special header information and the user-Agent header.
* @ Param request http request
* @ Return if the mobile phone feature rule is hit, the corresponding feature string is returned.
*/
Public static boolean isMobileDevice (HttpServletRequest request ){
Boolean pcFlag = false;
Boolean mobileFlag = false;
String via = request. getHeader ("");
String userAgent = request. getHeader ("user-agent ");
For (int I = 0;! = 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;
    }
}

This is

Write a static method. After calling, you can obtain the client from which the request is returned based on the returned value of isMobileDevice. It is quite convenient to use.

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.