Showing m.bing.com in the WP8 WebBrowser control

江枫思渺然 提交于 2019-12-20 04:11:13

问题


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!


回答1:


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;
}



回答2:


I don't have access to my emulator, but I tried it on my phone, and:

  1. The redirect doesn't happen when you "Prefer desktop version" and open m.bing.com. Warning: The mobile version isn't very pretty.

  2. Try disabling scripts on your WebBrowser, that could stop the redirect from happening.

  3. Any chance you could just use Google?



来源:https://stackoverflow.com/questions/20984875/showing-m-bing-com-in-the-wp8-webbrowser-control

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