Question

I have a simple WPF application with a webbrowser control. When I direct the control to load a page I'd like to have the control tell the server it's trying to load the page from that it's a mobile device and therefore load the smaller version of the page. I know a lot of sites just add mobile before the address, so google.com's mobile page is just mobile.google.com, but I'm wondering if there is a way to load the have the web server automatically direct my webbrowser control to the mobile version of the site? I feel like there should be a very simple way to do this, but I just can't figure it out :).

Thanks in advance!!!

Was it helpful?

Solution

This will generally be controlled by the site using the User Agent, which is not something you can change with the standard WebBrowser control. There is an alternative WPF webbrowser control, based on Chromium, which you can download from CodePlex. It might not support User Agent spoofing out of the box, but it's open source so you can manually change it in the code to an iPhone, Windows Mobile etc.

OTHER TIPS

Actually, the WebBrowser does support changing the User Agent via headers in the Navigate method:

this.yourWebBrowserControl.Navigate( new Uri( "http://www.yoursite.com" ), string.Empty, null, string.Format( "User-Agent: {0}", "Your user agent string here" ) );

I know this is 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 is a webbrowser control. This example will take you to the mobile bing website

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

Boydski's solution was not the right one for me. Best solution for this case:

Changing the user agent of the WebBrowser control

But sometime the "navigator.userAgent" is not set with header - solution. In this case you have to use UrlMkSetSessionOption. MSDN

// import .. use a internal static class like "Native" ;)
[DllImport("urlmon.dll", CharSet = CharSet.Ansi)]
internal static extern int UrlMkSetSessionOption(int dwOption, string pBuffer, int dwBufferLength, int dwReserved);

// usage
string userAgent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us)";

UrlMkSetSessionOption(0x10000002, null, 0, 0);

UrlMkSetSessionOption(0x10000001, userAgent, userAgent.Length, 0);

Proper user agent strings you can find here

Have fun!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top