Winform Webbrowser被识别为移动设备 [英] Winform Webbrowser being recognized as a mobile device

查看:297
本文介绍了Winform Webbrowser被识别为移动设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Winforms中的网络浏览器控件打开网站。尽管可以打开任何网站,但它会通过移动版本打开。我的Web浏览器被识别为移动设备。

解决方案

您可以使用



注意-作为更好的选择,为所有后续请求设置用户代理



作为一种选择,要为所有后续请求设置用户代理,您可以使用以下代码:

  [DllImport( urlmon.dll,CharSet = CharSet.Ansi)] 
私有静态extern int UrlMkSetSessionOption(int dwOption,字符串pBuffer,
int dwBufferLength,int dwReserved);
const int URLMON_OPTION_USERAGENT = 0x10000001;

string AdditionalHeaders = User-Agent:Mozilla / 5.0(Windows Phone 10.0; Android 6.0.1; +
Microsoft; Lumia 950 XL Dual SIM)AppleWebKit / 537.36(KHTML,例如Gecko) +
Chrome / 52.0.2743.116 Mobile Safari / 537.36 Edge / 15.15063\r\n;
private void Form1_Load(object sender,EventArgs e)
{
UrlMkSetSessionOption(URLMON_OPTION_USERAGENT,
AdditionalHeaders,AdditionalHeaders.Length,0);
webBrowser1.Navigate( http://google.com);
}


I'm trying to open websites through the web browser control inside winforms. Although whatever websites it does open, it opens through the mobile version. My web browser is being recognizeed as a mobile device.

解决方案

You can navigate to the page using Navigate method and pass a suitable User-Agent string as additionalHeaders parameter.

The trick is useful for sites which detect mobile mode at server-side based on user-agent string. For some sites which doesn't detect mobile mode and only have client-side responsive design which is based on browser size, you can resize the browser control to a suitable size to show mobile view.

Example

Here is an example of user agent string of Edge on Windows Phone 10.
You may want to use different user agent.

var additionalHeaders = "User-Agent:Mozilla/5.0 (Windows Phone 10.0; Android 6.0.1; " +
    "Microsoft; Lumia 950 XL Dual SIM) AppleWebKit/537.36 (KHTML, like Gecko) " +
    "Chrome/52.0.2743.116 Mobile Safari/537.36 Edge/15.15063\r\n";

this.webBrowser1.Navigate("http://www.stackoverflow.com", null, null, additionalHeaders);

As result you see stackoverflow site in mobile mode:

Note - As a better option, Set the user agent for all subsequent requests

As an option, to set the user agent for all the subsequent requests you can use the following code:

[DllImport("urlmon.dll", CharSet = CharSet.Ansi)]
private static extern int UrlMkSetSessionOption(int dwOption, string pBuffer,
    int dwBufferLength, int dwReserved);
const int URLMON_OPTION_USERAGENT = 0x10000001;

string additionalHeaders = "User-Agent:Mozilla/5.0 (Windows Phone 10.0; Android 6.0.1; " +
    "Microsoft; Lumia 950 XL Dual SIM) AppleWebKit/537.36 (KHTML, like Gecko) " +
    "Chrome/52.0.2743.116 Mobile Safari/537.36 Edge/15.15063\r\n";
private void Form1_Load(object sender, EventArgs e)
{
    UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, 
        additionalHeaders, additionalHeaders.Length, 0);
    webBrowser1.Navigate("http://google.com");
}

这篇关于Winform Webbrowser被识别为移动设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆