how to have the c# wpf webbrowser control load mobile version of websites

柔情痞子 提交于 2019-12-14 03:44:53

问题


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!!!


回答1:


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.




回答2:


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" ) );



回答3:


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




回答4:


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!



来源:https://stackoverflow.com/questions/1533429/how-to-have-the-c-sharp-wpf-webbrowser-control-load-mobile-version-of-websites

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!