Export (0) Print
Expand All

SOAP Code Samples (Multiple SourceTypes)

Bing

This topic contains code samples that produces a SOAP request for Multiple SourceTypes. For more information, see Working with SourceTypes (Bing, Version 2).

Requirements

  • A deployment computer with an Internet connection

  • The ability to send requests using the Simple Object Access Protocol (SOAP) 1.1 and the Hyper Text Transfer Protocol (HTTP 1.1)

  • The ability to parse SOAP and XML

Demonstrates

These code samples demonstrate how to:

  • Send a request to the Bing SOAP interface and the Web SourceType

  • Display the Bing response as results

The samples are written in both Visual Basic and C#.

Example

using System;
using System.Xml;

// This using directive assumes that the project's default namespace is
// "ApiSamples" and the name of the Bing API web reference is
// "net.bing.api". Modify this using directive as necessary.
using ApiSamples.net.bing.api;

// Bing API 2.0 code sample demonstrating the use of multiple
// SourceTypes over the SOAP Protocol.
static class MultipleSourceTypesSample
{
    // Replace the following string with the AppId you received from the
    // Bing Developer Center.
    const string AppId = "Insert your AppId here";

    static void Main()
    {
        // BingService implements IDisposable.
        using (BingService service = new BingService())
        {
            try
            {
                SearchRequest request = BuildRequest();

                // Send the request; display the response.
                SearchResponse response = service.Search(request);
                DisplayResponse(response);
            }
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                // A SOAP Exception was thrown. Display error details.
                DisplayErrors(ex.Detail);
            }
            catch (System.Net.WebException ex)
            {
                // An exception occurred while accessing the network.
                Console.WriteLine(ex.Message);
            }
        }
    }

    static SearchRequest BuildRequest()
    {
        SearchRequest request = new SearchRequest();

        // Common request fields (required)
        request.AppId = AppId;
        request.Query = "microsoft";
        request.Sources = new SourceType[]
        {
            SourceType.Web,
            SourceType.RelatedSearch
        };

        // Common request fields (optional)
        request.Version = "2.0";
        request.Market = "en-us";
        request.Adult = AdultOption.Moderate;
        request.AdultSpecified = true;
        request.Options = new SearchOption[]
        {
            SearchOption.EnableHighlighting
        };

        return request;
    }

    static void DisplayResponse(SearchResponse response)
    {
        // Display the RelatedSearch results header.
        Console.WriteLine("Bing API Version " + response.Version);
        Console.WriteLine(
            "RelatedSearch results for " + response.Query.SearchTerms);
        Console.WriteLine();

        // Display the RelatedSearch results.
        System.Text.StringBuilder builder = new System.Text.StringBuilder();
        foreach (RelatedSearchResult result in response.RelatedSearch.Results)
        {
            builder.Length = 0;
            builder.AppendLine(result.Title);
            builder.AppendLine(result.Url);

            DisplayTextWithHighlighting(builder.ToString());
            Console.WriteLine();
        }

        // Display the Web results header.
        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine("Web results for " + response.Query.SearchTerms);
        Console.WriteLine(
            "Displaying {0} to {1} of {2} results",
            response.Web.Offset + 1,
            response.Web.Offset + response.Web.Results.Length,
            response.Web.Total);
        Console.WriteLine();

        // Display the Web results.
        foreach (WebResult result in response.Web.Results)
        {
            builder.Length = 0;
            builder.AppendLine(result.Title);
            builder.AppendLine(result.Description);
            builder.AppendLine(result.Url);
            builder.Append("Last Crawled: ");
            builder.AppendLine(result.DateTime);

            DisplayTextWithHighlighting(builder.ToString());
            Console.WriteLine();
        }
    }

    static void DisplayTextWithHighlighting(string text)
    {
        // Write text to the standard output stream, changing the console
        // foreground color as highlighting characters are encountered.
        foreach (char c in text.ToCharArray())
        {
            if (c == '\uE000')
            {
                // If the current character is the begin highlighting
                // character (U+E000), change the console foreground color
                // to green.
                Console.ForegroundColor = ConsoleColor.Green;
            }
            else if (c == '\uE001')
            {
                // If the current character is the end highlighting
                // character (U+E001), revert the console foreground color
                // to gray.
                Console.ForegroundColor = ConsoleColor.Gray;
            }
            else
            {
                Console.Write(c);
            }
        }
    }

    static void DisplayErrors(XmlNode errorDetails)
    {
        // Add the default namespace to the namespace manager.
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(
            errorDetails.OwnerDocument.NameTable);
        nsmgr.AddNamespace(
            "api",
            "http://schemas.microsoft.com/LiveSearch/2008/03/Search");

        XmlNodeList errors = errorDetails.SelectNodes(
            "./api:Errors/api:Error",
            nsmgr);

        if (errors != null)
        {
            // Iterate over the list of errors and display error details.
            Console.WriteLine("Errors:");
            Console.WriteLine();
            foreach (XmlNode error in errors)
            {
                foreach (XmlNode detail in error.ChildNodes)
                {
                    Console.WriteLine(detail.Name + ": " + detail.InnerText);
                }

                Console.WriteLine();
            }
        }
    }
}

Show:
© 2014 Microsoft