This Console application uses default input parameters and a hard-coded query string to demonstrate the basic functionality of the Live Search Web Service API.
Tip |
|
You can use this sample application to run any of the examples in the API Reference by copying the example code (using the Copy Code button in the topic) and pasting it into the sample application between the comments. In each case, you will need to use your own AppID and rebuild the sample application to run the example code. |
To set up the project in Microsoft Visual Studio .NET 2005
-
Open Microsoft Visual Studio .NET 2005 and, from the File menu, choose New | Project. The New Project dialog box appears.
-
In the Project types pane, choose Visual C# and then choose Windows.
-
In the Templates pane, choose Console Application.
-
Type the name of the project, ConsoleSampleWebSearch, in the Name text box and click OK. Visual Studio creates a new project.
-
In the Solution Explorer, right-click References and, from the pop-up menu, select Add Web Reference.
-
Type the following address in the URL text box: http://soap.search.msn.com/webservices.asmx?wsdl.
-
Click Go.
-
In the Web reference name text box, type LiveSearch and click Add Reference.
To add the source code to the project
-
Replace the source code in the file Program.cs with the source code in the Example section. To do this:
-
Click Copy Code to place the code in the Windows Clipboard.
-
Open
Program.cs from the Solution Explorer, place your cursor anywhere in the file, and press Ctrl + A.
-
Press Ctrl + V to paste the code into the Visual Studio source file, overwriting the template-generated code.
-
Search for and replace the text YOUR_APP_ID_GOES_HERE with the AppID you generated from the Developer Provisioning System.
-
Press F5 to run the application in Debug mode.
Requirements
-
Microsoft Visual Studio .NET 2005 or Microsoft Visual C# .NET 2005.
-
Internet connection.
-
Application ID. (See Getting Started with the Live Search API for more information on obtaining an AppID for use with the Live Search Web Service.)
Demonstrates
A basic Web search using the keywords live search and default input parameters.
Example
//THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
//TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//
//Copyright (C) 2007 Microsoft Corporation. All rights reserved.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Services.Protocols;
using System.Net;
using ConsoleSampleWebSearch.LiveSearch;
namespace ConsoleSampleWebSearch
{
class Program
{
static void Main(string[] args)
{
/* TIP: To run any of the samples featured in the API Reference, remove the code in the try...catch
* block and replace it with the code from the example topic you want to run. */
try
{
MSNSearchService s = new MSNSearchService();
SearchRequest searchRequest = new SearchRequest();
int arraySize = 1;
SourceRequest[] sr = new SourceRequest[arraySize];
sr[0] = new SourceRequest();
sr[0].Source = SourceType.Web;
searchRequest.Query = "live search";
searchRequest.Requests = sr;
// Enter the Application ID, in double quotation marks, supplied by the
// Developer Provisioning System, as the value of the AppID on the SearchRequest.
searchRequest.AppID = "YOUR_APP_ID_GOES_HERE";
searchRequest.CultureInfo = "en-US";
SearchResponse searchResponse;
searchResponse = s.Search(searchRequest);
foreach (SourceResponse sourceResponse in searchResponse.Responses)
{
Result[] sourceResults = sourceResponse.Results;
if (sourceResponse.Total > 0)
{
Console.WriteLine(sourceResponse.Source.ToString() + " - Total Results: " + sourceResponse.Total.ToString());
Console.WriteLine();
}
foreach (Result sourceResult in sourceResults)
{
if ((sourceResult.Title != null) && (sourceResult.Title != String.Empty))
Console.WriteLine("Title: " + sourceResult.Title);
if ((sourceResult.Description != null) && (sourceResult.Description != String.Empty))
Console.WriteLine("Description: " + sourceResult.Description);
if ((sourceResult.Url != null) && (sourceResult.Url != String.Empty))
Console.WriteLine("URL: " + sourceResult.Url);
Console.WriteLine("*****************************************************");
}
}
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
catch (System.Web.Services.Protocols.SoapException fault)
{
Console.WriteLine(fault.Detail.InnerText.ToString());
Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
catch (System.Net.WebException webx)
{
Console.WriteLine(webx.ToString());
}
/* This is the end of the try...catch block. */
}
}
}