[英]How do I modify the UserAgent in webkit-sharp webview object?
[英]How do I check if the useragent is an ipad or iphone?
我正在使用 C# asp.net 網站。
如何檢查用戶是否使用 ipad 或 iphone? 我如何檢查平台?
例如,如果用戶從 ipad 進入網站,我想顯示“你好 ipad 用戶”
2020 年 7 月 7 日更新:看起來 Apple 刪除了iPad
這個詞,現在改用Macintosh
更新:由於 iPad 用戶代理包含 @Rob Hruska 提到的iPhone
一詞:
Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10
和 iPhone 用戶代理是這樣的:
Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7
檢查單詞iPhone;
是正確的iPhone;
或iPad;
識別設備:
var userAgent = HttpContext.Current.Request.UserAgent.ToLower();
if (userAgent.Contains("iphone;"))
{
// iPhone
}
else if (userAgent.Contains("ipad;") || userAgent.Contains("macintosh;"))
{
// iPad
}
else
{
// Think Different ;)
}
對於 iPad 用戶代理是這樣的:
Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10
對於 iPhone,它類似於:
Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3
更多取決於版本以及 iPhone 3 或 4
所以最好按照另一個答案的建議對 iPhone 和 iPad 進行子字符串搜索
這些設備的用戶代理包括“iPod”、“iPad”或“IPhone”(視情況而定)。 請注意,有多個用戶代理正在運行,因此完全匹配是不明智的 - 但請從您的設備上查看 http://whatsmyuseragent.com
因此,請檢查標題中的用戶代理。
我會先嘗試 WURFL http://wurfl.sourceforge.net/
他們有 .NET API 和非常好的代碼示例。 http://wurfl.sourceforge.net/dotnet_index.php
可以幫助您的類稱為WURFLManager並具有以下方法:
使用 WURFL http://wurfl.sourceforge.net/dotnet_index.php
如果您使用 asp.net mvc,則可以使用 ActionFilter
public class MobileActionFilterAttribute : ActionFilterAttribute
{
// The WURFL database contains information about a huge number of devices and mobile browsers.
// http://wurfl.sourceforge.net/
// http://wurfl.sourceforge.net/dotnet_index.php
// http://wurfl.sourceforge.net/help_doc.php
private static readonly IWURFLManager WurflManager;
static MobileActionFilterAttribute ()
{
IWURFLConfigurer configurer = new ApplicationConfigurer();
WurflManager = WURFLManagerBuilder.Build(configurer);
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpRequestBase request = filterContext.RequestContext.HttpContext.Request;
// We don't have ARR server for dev environment, so we still need to check to see if the current domain name is the mobile site.
if (request.Url.AbsoluteUri.StartsWith(SiteConfiguration.Current.MobileSiteAddress, StringComparison.OrdinalIgnoreCase))
{
return;
}
// Creates a WURFLRequest object from an ASP.NET HttpRequest object
WURFLRequest wurflRequest = WURFLRequestFactory.CreateRequest(HttpContext.Current.Request);
// Indicates whether the current user agent string refers to a desktop agent.
if (wurflRequest.IsDesktopRequest)
return;
// Get the information about the device
IDevice deviceInfo = WurflManager.GetDeviceForRequest(wurflRequest);
// Tells you if a device is a tablet computer (iPad and similar, regardless of OS)
bool isTablet = string.Equals(deviceInfo.GetCapability("is_tablet") ?? string.Empty, "true", StringComparison.OrdinalIgnoreCase);
if (isTablet)
{
// so we don't show the mobile site for iPad.
return;
}
// Indicates whether the current user agent string refers to a mobile device.
bool isMobileRequest = wurflRequest.IsMobileRequest;
// Tells you if a device is wireless or not. Specifically a mobile phone or a PDA are considered wireless devices, a desktop PC or a laptop are not
bool isWirelessDevice = string.Equals(deviceInfo.GetCapability("is_wireless_device") ?? string.Empty, "true", StringComparison.InvariantCultureIgnoreCase);
if (isMobileRequest && isWirelessDevice)
{
// we can redirect to the mobile site!
filterContext.Result = new RedirectResult(SiteConfiguration.Current.MobileSiteAddress);
}
}
}
還有 51Degrees.Mobi Steve Sanderson 在他的博客上介紹了如何做到這一點http://blog.stevensanderson.com/2010/12/17/using-51degreesmobi-foundation-for-accurate-mobile-browser-detection-on -aspnet-mvc-3/
51Degrees.Mobi Foundation 是一個開源 .NET 項目,它增強了 Request.Browser,因此它可以從無線通用資源文件 (WURFL) 中獲取信息——這是最全面和最新的移動設備信息數據庫之一。 好消息是 51Degrees.Mobi Foundation 現在可作為 NuGet 包使用,因此安裝和更新非常容易。
您可以通過獲取 UserAgent 來實現
string ua = Request.UserAgent;
if (ua != null && (ua.Contains("iPhone") || ua.Contains("iPad")))
{
...
...
...
}
小心Windows手機! 出於某種奇怪的原因,許多 Windows 手機在用戶代理中說“像 iPhone”。 所以你想檢查:
public bool IsIPhone
{
get
{
if (!UserAgent.ToUpper().Contains("LIKE IPHONE"))
{
return UserAgent.ToUpper().Contains("IPHONE");
}
return false;
}
}
Windows 手機用戶代理示例(來自 Lumia 735):
"Mozilla/5.0 (Mobile; Windows Phone 8.1; Android 4.0; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 735) like iPhone OS 7_0_3 Mac OS X AppleWebKit/537 (KHTML, like Gecko ) 移動 Safari/537"
從iOS 13 開始,用戶代理更改為 Mac OS,例如:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15
private static final Pattern IPHONE_AGENT = Pattern.compile(".*iPad.*|.*iPhone.*|.*iPod.*");
String userAgent = request.getHeader("User-Agent");
if (userAgent != null && IPHONE_AGENT.matcher(userAgent).matches()) {
// do something
}
您可以從Request.UserAgent
獲取客戶端操作系統數據,獲取操作系統名稱和操作系統版本。
public static string GetClientOS(string ua, string platform)
{
if (ua.Contains("Android"))
return string.Format("Android {0}", GetMobileVersion(ua, "Android"));
if (ua.Contains("iPad"))
return string.Format("iPad OS {0}", GetMobileVersion(ua, "OS"));
if (ua.Contains("iPhone"))
return string.Format("iPhone OS {0}", GetMobileVersion(ua, "OS"));
if (ua.Contains("Linux") && ua.Contains("KFAPWI"))
return "Kindle Fire";
if (ua.Contains("RIM Tablet") || (ua.Contains("BB") && ua.Contains("Mobile")))
return "Black Berry";
if (ua.Contains("Windows Phone"))
return string.Format("Windows Phone {0}", GetMobileVersion(ua, "Windows Phone"));
if (ua.Contains("Mac OS"))
return "Mac OS";
if (ua.Contains("Windows NT 5.1") || ua.Contains("Windows NT 5.2"))
return "Windows XP";
if (ua.Contains("Windows NT 6.0"))
return "Windows Vista";
if (ua.Contains("Windows NT 6.1"))
return "Windows 7";
if (ua.Contains("Windows NT 6.2"))
return "Windows 8";
if (ua.Contains("Windows NT 6.3"))
return "Windows 8.1";
if (ua.Contains("Windows NT 10"))
return "Windows 10";
return platform + (ua.Contains("Mobile") ? " Mobile " : "");
}
public static string GetMobileVersion(string userAgent, string device)
{
var temp = userAgent.Substring(userAgent.IndexOf(device) + device.Length).TrimStart();
var version = string.Empty;
foreach (var character in temp)
{
var validCharacter = false;
int test = 0;
if (int.TryParse(character.ToString(), out test))
{
version += character;
validCharacter = true;
}
if (character == '.' || character == '_')
{
version += '.';
validCharacter = true;
}
if (validCharacter == false)
break;
}
return version;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.