Bing: Performing a basic web search over SOAP with the new Bing API 2.0

Posted by Adrian Mon, 06 Jul 2009 11:42:00 GMT

Here's a very quick code sample that you can use to add Bing API search capabilities to your .NET application. You will need to sign-up and get an API key for this to work, but it's free and you can use an existing Live ID if you have one (for example, for MSDN).

The example here was taken from a Console application. You will need to integrate it in to your own application (if you just want to see it working, create your own console application and paste it in to the main function).

You need to add a reference to the SOAP service to your application. Right click the 'References' folder and choose 'Add Service Reference'. The address is: "http://api.bing.net/search.wsdl?Version=2.2&AppID=" with your AppId appended to the end.

string AppId = ""; // enter your AppId key here!
string query = "michael jackson";

using (BingPortTypeClient client = new BingPortTypeClient())
{
  var request = new SearchRequest();

  request.AppId = AppId;
  request.Sources = new SourceType[] { SourceType.Web };
  request.Query = query;

  SearchResponse response = client.Search(request);

  if (response.Web != null && response.Web.Results.Count() > 0)
  {
    var pos = 0;

    foreach (var r in response.Web.Results)
    {
      pos++;

      Console.WriteLine(pos + ". " + r.Title);
      Console.WriteLine(r.Url);
      Console.WriteLine(r.Description);
      Console.WriteLine();
    }
  }
}

Console.ReadLine();

The reason I post this code here is that Microsoft haven't yet updated MSDN with any code samples for the new API yet, and it took me a while to realise that the SOAP client was BingPortTypeClient.

Related: Using the Bing XML Search API with Ruby


About

We are a small British company that produces business-oriented software and solutions. These articles are a product of our daily work - information that we think might be useful to share. We hope you find them useful.

Our Software

These are some of our products. Several are open source, some are web-based and others are proprietary:

Categories

Archives

Syndicate

ml> ._trackPageview(); } catch(err) {} ml> l> pageTracker._trackPageview(); } catch(err) {} ml> ._trackPageview(); } catch(err) {} ml> l>