在WP8 WebBrowser控件中显示m.bing.com [英] Showing m.bing.com in the WP8 WebBrowser control

查看:174
本文介绍了在WP8 WebBrowser控件中显示m.bing.com的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将bing.com加载到Windows Phone 8上的WebBrowser控件时遇到问题.看来这样做会启动WP8 Search App(与按手机上的Search按钮相同).问题是,一旦您单击该搜索应用程序中的结果,便不会将您带回到原始应用程序-它会通过IE来显示结果.这对我不起作用,并且似乎是WebBrowser行为中的一个巨大缺陷(IMO).

似乎确实有一些应用程序能够在不启动手机的搜索应用程序的情况下成功显示bing.com,例如Image Downloader Free.还有另一个,但是我不记得那是什么...

经过研究,我发现WebBrowser_Navigating事件在进入bing.com时被触发3次:首先请求用户输入的URL(www.bing.com),然后将其重定向到 http://wp.m.bing.com/?mid=10006 ,然后重定向到bing://home/?mid = 10006.

防止转发到Bing搜索应用程序非常简单,只需将其添加到导航事件中即可:

e.Cancel = (e.Uri.Scheme == "bing");

问题是,然后它仅显示Bing搜索页面占位符,该占位符显示为"Bing Search",并且具有一个链接为"Backing Bing search",该链接什么都不做(通常会重新启动Bing Search应用)./p>

我有一些想法,但是我不确定它们是否可行.

  • 在WP8 WebBrowser控件中,是否可以伪造用户代理?
  • 是否可以删除或添加WebBrowser.Uri.Flags属性中的一项以影响Bing.com处理请求的方式?
  • 如果这些都不起作用,我可以在Web服务器上简单地创建一个虚拟页面,将所有bing.com请求重定向到该页面,然后让它使用卡编码的用户代理获取m.bing.com的首页. .我确实很想避免这样做.从最终用户的角度来看,他们永远不会知道,但是我只是增加了一层全新的开销,维护和资源方面的内容.

如果您感兴趣,请附加在WebBrowser.Navigating事件中发生的3个请求之间EventArgs对象的差异:

请求1(bing.com)->请求2(转发至wp.m.bing.com/?mid=10006)

请求2(转发至wp.m.bing.com/?mid=10006)->请求3(转发至bing://home/?mid = 10006)

tl; dr有没有人知道一种防止w​​ww.bing.com导致搜索应用程序在我的应用程序的WebBrowser控件中启动的方法?

谢谢!

解决方案

我不知道是否有更好的方法来解决此问题,但我找到了解决方案.单击后退"按钮时,它还无法正常工作,因此如果/当我找到更可靠的解决方案时,我将更新我的答案.我仍然认为这是WP8中WebBrowser控件的一个大缺陷.

这是代码:

private bool _customHeaderRequest = false;

private void MainBrowser_Navigating(object sender, NavigatingEventArgs e)
{
    string host = e.Uri.Host.ToLowerInvariant().Trim();

    if ((host == "bing.com" || host.EndsWith(".bing.com")) && !_customHeaderRequest)
    {
        e.Cancel = true;

        Dispatcher.BeginInvoke(() =>
            MainBrowser.Navigate(e.Uri, null,
                "User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; NOKIA; Lumia 710)\r\n"));

        _customHeaderRequest = true;
        return;
    }

    _customHeaderRequest = false;
}

private void MainBrowser_Navigated(object sender, NavigationEventArgs e)
{
    _customHeaderRequest = false;
}

I'm having a problem getting bing.com to load in a WebBrowser control on Windows Phone 8. It seems that doing that will launch the WP8 Search App (same as pressing the Search button on the phone). The problem is, once you click a result in that search app, it doesn't take you back to your original app - it goes to IE to show the result. This isn't going to work for me and seems to be a massive flaw (IMO) in the WebBrowser behavior.

There does seem to be a few apps out there that have succeeded in being able to show bing.com without launching the phone's search app - for example Image Downloader Free. There was another one, but I can't remember what it was...

After some research, I've found that the WebBrowser_Navigating event gets fired 3 times when going to bing.com: first request to the user-entered URL (www.bing.com), it then gets redirected to http://wp.m.bing.com/?mid=10006, then it redirects to bing://home/?mid=10006.

Preventing it from forwarding to the Bing search app is quite simple, just add this to the Navigating event:

e.Cancel = (e.Uri.Scheme == "bing");

The problem is, it then only shows the Bing search page place holder which says "Bing Search" and has a link that says "Back to Bing search" which does nothing (it would typically relaunch the Bing Search app).

I have a few thoughts, but I'm not sure how feasible they are.

  • In the WP8 WebBrowser control, is it possible to fake the User Agent?
  • Can one of the items in the WebBrowser.Uri.Flags property be removed or added to affect the way Bing.com handles the request?
  • If none of those work, I can simply create a dummy page on my web server, redirect all bing.com requests to it, and have it grab the m.bing.com front page with a card-coded user-agent. I really would like to avoid having to do this option though. From an End-User perspective, they would never know, but I just added a whole new layer of overhead, maintenance and resource-wise.

If you're interested, attached are the diff's for the EventArgs object between the 3 requests that occur in the WebBrowser.Navigating event:

Request 1 (bing.com) -> Request 2 (forwarded to wp.m.bing.com/?mid=10006)

Request 2 (forwarded to wp.m.bing.com/?mid=10006) -> Request 3 (forwarded to bing://home/?mid=10006)

tl;dr Does anyone know of a way to prevent www.bing.com from causing the search app to launch in the WebBrowser control in my application?

Thank you!

解决方案

I don't know if there's a better way to handle this, but I found a solution. I haven't gotten it to work perfectly when the back button is clicked, so I will update my answer if/when I found a more solid solution. I still think this is a big flaw in the WebBrowser control in WP8.

Here is the code:

private bool _customHeaderRequest = false;

private void MainBrowser_Navigating(object sender, NavigatingEventArgs e)
{
    string host = e.Uri.Host.ToLowerInvariant().Trim();

    if ((host == "bing.com" || host.EndsWith(".bing.com")) && !_customHeaderRequest)
    {
        e.Cancel = true;

        Dispatcher.BeginInvoke(() =>
            MainBrowser.Navigate(e.Uri, null,
                "User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; NOKIA; Lumia 710)\r\n"));

        _customHeaderRequest = true;
        return;
    }

    _customHeaderRequest = false;
}

private void MainBrowser_Navigated(object sender, NavigationEventArgs e)
{
    _customHeaderRequest = false;
}

这篇关于在WP8 WebBrowser控件中显示m.bing.com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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