简体   繁体   中英

Make a web browser navigate to mobile phone site

I'm building a windows form using a web browser. I have a URL and I want the web browser to display the mobile site. For example:

If I type: www.facebook.com, I want the web browser to navigate to: m.facebook.com. How do I pretend to be a mobile phone? I don't know what I need to do to solve my problem. I'm using Visual Studio 2012 - winform - c#.

I tried this:

WebClient client = new WebClient();
client.Headers["User-Agent"] = "myUserAgentString";

I know this is about a year old, but this is an easy thing to do:

First you need the user agent string and it needs to be string formatted. This is how I do it below

wbMobile.Navigate(new Uri("http://m.bing.com/", UriKind.RelativeOrAbsolute), string.Empty, null, string.Format("User-Agent: {0}", "Opera/9.80 (J2ME/MIDP; Opera Mini/9 (Compatible; MSIE:9.0; iPhone; BlackBerry9700; AppleWebKit/24.746; U; en) Presto/2.5.25 Version/10.54"));

This will navigate to the webpage using a mobile user agent string and allow you to view mobile websites on the fly in the webbrowser control

I used something like this: (JavaScript)

<script>
if (document.location.search.indexOf('skipmobile') >= 0) {
    document.cookie = 'skipmobile=1';
}
else if ((document.location.hostname.match(/\.mobi$/) || screen.width < 799)
&& document.cookie.indexOf('skipmobile') == -1)
{
    document.location = 'm/';
}
</script>

In other words, if screen display is under 799px, redirect user to the mobile version, which was mysite.com/m/

Here is the user agent string for iphone ios5

Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3

From: What is the iOS 5.0 user agent string?

You can get the code you need from detectmobilebrowsers.com

You can the use F12 developer tools in IE 10+ to emulate mobile browsers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM