1

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) enter image description here

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

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!

Adam Plocher
  • 13,994
  • 6
  • 46
  • 79

2 Answers2

2

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;
}
Adam Plocher
  • 13,994
  • 6
  • 46
  • 79
1

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?

miguelarcilla
  • 1,426
  • 1
  • 20
  • 38
  • Hi Miguel, thanks for the response. Regarding your first item: true it does prevent the Search App from opening in IE, but it doesn't seem to affect the WebBrowser control. I've tried with scripts enabled and disabled (they're disabled by default). They seem to redirect based User Agent and HTTP 30x response codes. Regarding Google, I want my users to be able to go anywhere. The app is meant to scan the HTML of the current URL where the user is. I've posted a solution, but it's not perfect. Thanks again. – Adam Plocher Jan 08 '14 at 06:24
  • 1
    @AdamPlocher Hey, we all have to hack around something sometimes. Hopefully MS can get that issue fixed, I can imagine how much of a hassle that must be. You could try posting this in the Support forums (http://answers.microsoft.com/en-us/winphone/forum/wp8?tab=QnA&status=answered&sort=HelpfulCount&dir=Desc) and see if they can report it as a defect. Best of luck :) – miguelarcilla Jan 08 '14 at 06:33