服务器判断客户端为移动端还是PC端

 1 public void ProcessRequest(HttpContext context)
 2         {
 3             context.Response.ContentType = "text/html";
 4             HttpBrowserCapabilities bc = context.Request.Browser;
 5             //context.Response.Write("<p>Browser Capabilities:</p>");
 6             //context.Response.Write("Type = " + bc.Type + "<br>");
 7             //context.Response.Write("Name = " + bc.Browser + "<br>");
 8             //context.Response.Write("Version = " + bc.Version + "<br>");
 9             //context.Response.Write("Major Version = " + bc.MajorVersion + "<br>");
10             //context.Response.Write("Minor Version = " + bc.MinorVersion + "<br>");
11             //context.Response.Write("Platform = " + bc.Platform + "<br>");
12             //context.Response.Write("Is Beta = " + bc.Beta + "<br>");
13             //context.Response.Write("Is Crawler = " + bc.Crawler + "<br>");
14             //context.Response.Write("Is AOL = " + bc.AOL + "<br>");
15             //context.Response.Write("Is Win16 = " + bc.Win16 + "<br>");
16             //context.Response.Write("Is Win32 = " + bc.Win32 + "<br>");
17             //context.Response.Write("Supports Frames = " + bc.Frames + "<br>");
18             //context.Response.Write("Supports Tables = " + bc.Tables + "<br>");
19             //context.Response.Write("Supports Cookies = " + bc.Cookies + "<br>");
20             //context.Response.Write("Supports VB Script = " + bc.VBScript + "<br>");
21             //context.Response.Write("Supports JavaScript = " + bc.JavaScript + "<br>");
22             //context.Response.Write("Supports Java Applets = " + bc.JavaApplets + "<br>");
23             //context.Response.Write("Supports ActiveX Controls = " + bc.ActiveXControls + "<br>");
24             //context.Response.Write("CDF = " + bc.CDF + "<br>");
25             //context.Response.Write(bc.SupportsXmlHttp);
26             //context.Response.Write(HttpContext.Current.Request.UserAgent + "<br>");
27             string ss = HttpContext.Current.Request.UserAgent;
28             context.Response.Write(ss+"<br>");
29             string m = "mobile";
30             int res= ss.ToLower().IndexOf(m, 0, ss.Length);
31             if (res<=0)
32             {
33                 context.Response.Write("Pc端");
34             }
35             else
36             {
37                 context.Response.Write("你使用的是手机端");
38             }
39 
40 
41         }

通过UserAgent获得客户端提交上来的消息,存储为字符串;

通过匹配字符串,如果字符串中包含mobile,则返回手机端,否则为PC端

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。