【问题标题】:Make a web browser navigate to mobile phone site让网络浏览器导航到手机网站
【发布时间】:2014-05-03 22:32:41
【问题描述】:

我正在使用网络浏览器构建一个 Windows 窗体。 我有一个 URL,我希望网络浏览器显示移动网站。 例如:

如果我输入:www.facebook.com, 我希望网络浏览器导航到:m.facebook.com。 怎么伪装成手机? 我不知道我需要做什么来解决我的问题。 我正在使用 Visual Studio 2012 - winform - c#。

我试过这个:

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

【问题讨论】:

  • 好的。所以你试过了。发生了什么? "myUserAgentString" 是您使用的实际值吗?您尝试过哪些其他用户代理字符串

标签: c# asp.net winforms web


【解决方案1】:

我知道这大约有一年了,但这很容易做到:

首先你需要用户代理字符串并且它需要被格式化。 下面我就是这样做的

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

这将使用移动用户代理字符串导航到网页,并允许您在网络浏览器控件中即时查看移动网站

【讨论】:

    【解决方案2】:

    您可以从detectmobilebrowsers.com获取您需要的代码

    您可以在 IE 10+ 中使用 F12 developer tools 来模拟移动浏览器。

    【讨论】:

      【解决方案3】:

      这是 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
      

      发件人:What is the iOS 5.0 user agent string?

      【讨论】:

      • 你能说得更详细一点吗?你能写出我需要写的东西吗?
      • @user3461486 client.Headers["User-Agent"] = "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"
      • 还是不行。我如何将您给我的内容与网站的 http 请求(用于请求源代码)结合起来?
      • 使用诸如wireshark之​​类的东西来检查移动设备发送的全套标头。发送同样的内容,您应该会收到该网站的移动版本。
      【解决方案4】:

      我用过这样的东西:(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>
      

      也就是说,如果屏幕显示低于 799px,则将用户重定向到移动版本,即 mysite.com/m/

      【讨论】:

      • 我想假装是一部手机,而不是检查一个网站是否是用于手机的......而且我的代码中没有使用javascript......
      猜你喜欢
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 2011-12-18
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多