伺服器判斷用戶端為移動端還是PC端

來源:互聯網
上載者:User

標籤:

 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             else36             {37                 context.Response.Write("你使用的是手機端");38             }39 40 41         }

通過UserAgent獲得用戶端提交上來的訊息,儲存為字串;

通過匹配字串,如果字串中包含mobile,則返回手機端,否則為PC端

伺服器判斷用戶端為移動端還是PC端

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.