Share via


SOAP Code Samples (Image SourceType)

This topic contains code samples that produces a SOAP request for the Image SourceType. For more information, see Image SourceType (Bing, Version 2.0).

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

Imports System
Imports System.Xml

' This Imports statement assumes that the project's root namespace is
' "ApiSamples" and the name of the Bing API web reference is
' "net.bing.api". Modify this Imports statement as necessary.
Imports ApiSamples.net.bing.api

' Bing API 2.0 code sample demonstrating the use of the
' Image SourceType over the SOAP Protocol.
Class ImageSample

    ' Replace the following string with the AppId you received from the
    ' Bing Developer Center.
    Const AppId As String = "Insert your AppId here"

    Shared Sub Main()
        ' BingService implements IDisposable.
        Using service As BingService = New BingService
            Try
                Dim request As SearchRequest = BuildRequest()

                ' Send the request; display the response.
                Dim response As SearchResponse = service.Search(request)
                DisplayResponse(response)
            Catch ex As System.Web.Services.Protocols.SoapException
                ' A SOAP Exception was thrown. Display error details.
                DisplayErrors(ex.Detail)
            Catch ex As System.Net.WebException
                ' An exception occurred while accessing the network.
                Console.WriteLine(ex.Message)
            End Try
        End Using
    End Sub

    Shared Function BuildRequest() As SearchRequest
        Dim request As New SearchRequest

        With request
            ' Common request fields (required)
            .AppId = AppId
            .Query = "xbox site:microsoft.com"
            .Sources = New SourceType() {SourceType.Image}

            ' Common request fields (optional)
            .Version = "2.0"
            .Market = "en-us"
            .Adult = AdultOption.Moderate
            .AdultSpecified = True

            .Image = New ImageRequest
            With .Image
                ' Image-specific request fields (optional)
                .Count = 10
                .CountSpecified = True
                .Offset = 0
                .OffsetSpecified = True
            End With
        End With

        Return request
    End Function

    Shared Sub DisplayResponse(ByVal response As SearchResponse)
        ' Display the results header.
        With response
            Console.WriteLine("Bing API Version " & .Version)
            Console.WriteLine("Image results for " & .Query.SearchTerms)
            With .Image
                Console.WriteLine( _
                    "Displaying {0} to {1} of {2} results", _
                    .Offset + 1, _
                    .Offset + .Results.Length, _
                    .Total)
                Console.WriteLine()
            End With
        End With

        ' Display the Image results.
        Dim result As ImageResult
        For Each result In response.Image.Results
            With result
                Console.WriteLine(.MediaUrl)
                Console.WriteLine("Page Title: " & .Title)
                Console.WriteLine("Page URL: " & .Url)
                Console.WriteLine("Dimensions: " & .Width & "x" & .Height)
                Console.WriteLine("Thumbnail URL: " & .Thumbnail.Url)
                Console.WriteLine()
            End With
        Next
    End Sub

    Shared Sub DisplayErrors(ByVal errorDetails As XmlNode)
        ' Add the default namespace to the namespace manager.
        Dim nsmgr As New XmlNamespaceManager( _
            errorDetails.OwnerDocument.NameTable)
        nsmgr.AddNamespace( _
            "api", _
            "http://schemas.microsoft.com/LiveSearch/2008/03/Search")

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

        If (Not errors Is Nothing) Then
            ' Iterate over the list of errors and display error details.
            Console.WriteLine("Errors:")
            Console.WriteLine()
            Dim [error] As XmlNode
            For Each [error] In errors
                Dim detail As XmlNode
                For Each detail In [error].ChildNodes
                    Console.WriteLine(detail.Name & ": " & detail.InnerText)
                Next
                Console.WriteLine()
            Next
        End If
    End Sub

End Class
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 the
// Image SourceType over the SOAP Protocol.
static class ImageSample
{
    // 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 = "xbox site:microsoft.com";
        request.Sources = new SourceType[] { SourceType.Image };

        // Common request fields (optional)
        request.Version = "2.0";
        request.Market = "en-us";
        request.Adult = AdultOption.Moderate;
        request.AdultSpecified = true;

        // Image-specific request fields (optional)
        request.Image = new ImageRequest();
        request.Image.Count = 10;
        request.Image.CountSpecified = true;
        request.Image.Offset = 0;
        request.Image.OffsetSpecified = true;

        return request;
    }

    static void DisplayResponse(SearchResponse response)
    {
        // Display the results header.
        Console.WriteLine("Bing API Version " + response.Version);
        Console.WriteLine("Image results for " + response.Query.SearchTerms);
        Console.WriteLine(
            "Displaying {0} to {1} of {2} results",
            response.Image.Offset + 1,
            response.Image.Offset + response.Image.Results.Length,
            response.Image.Total);
        Console.WriteLine();

        // Display the Image results.
        foreach (ImageResult result in response.Image.Results)
        {
            Console.WriteLine(result.MediaUrl);
            Console.WriteLine("Page Title: " + result.Title);
            Console.WriteLine("Page URL: " + result.Url);
            Console.WriteLine(
                "Dimensions: "
                + result.Width
                + "x"
                + result.Height);
            Console.WriteLine("Thumbnail URL: " + result.Thumbnail.Url);
            Console.WriteLine();
        }
    }

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