在ASP.NET Web Application中通过SOAP协议调用Bing搜索服务

  本文介绍了如何在ASP.NET Web Application中将Bing搜索作为Web Service来使用,并通过HTTP的SOAP协议在ASP.NET Web Application中调用Bing搜索功能。当然,我们也可以使用XML和JSON来调用Bing搜索服务,稍后会给出一些相关的例子。

  本文中我将向大家演示下面这些内容:

  • 创建一个Web Application与Bing搜索服务进行交互
  • 添加Bing搜索服务的引用
  • 使用Bing SourceTypes演示在线搜索功能

Bing搜索服务介绍

  将Bing搜索功能作为Web Service来使用可以在我们的应用程序中简化许多较为复杂的功能,如创建索引、建立相关逻辑、解决数据存储等问题,本文旨从在你的应用程序中添加Bing Web Service开始。

  什么是Web Service,想必大家都已经比较清楚了,它是Web Server上的一个程序组件,能够被客户端应用程序通过HTTP协议在Web上进行调用。有关Web Service的详细介绍,你可以查看MSDN上的文章Using ASP.NET Web Services.

  使用Bing搜索服务你可以在你的程序中集成下列功能:

  • 从Internet上获取有用的信息
  • 在你的应用程序中添加广告内容
  • 提高和增强搜索功能
  • 查找特定位置信息
  • 翻译条款和文本块

  你可以通过Bing API来调用Bing Service的各种功能。上面提到的这些功能都或多或少地使用到了Bing API SourceTypes。一个SourceType是一个可以通过Bing API进行访问的信息源,有关所有这些SourceTypes的描述可以访问MSDN:About the Bing API

 

先决条件

  要完成本文中提到的这些功能,你必须具备下面这些条件:

  • Visual Studio 2010(稍低版本的VS也可以,如Visual Studio 2008)
  • 能够通过SOAP协议(1.1)和HTTP协议(1.1)向服务器端发送请求
  • 能够解析SOAP和XML

创建Web Application

  现在我们开始展示如何创建一个Web Application并使用Bing搜索服务。

首先打开Visual Studio并创建一个普通的Web Application工程。如果你使用的是Visual Studio 2010,建议创建ASP.NET Empty Web Application类型的工程。  2010-7-19 14-57-43  然后我们需要在工程中添加Bing Search Service的Web引用。我们通过Web Service发现程序以获得Web服务的位置和描述信息,在Visual Studio中,Web Service发现程序是一个WSDL(Web Services Description Language)格式的XML文件,用来从Web站点中找到Web服务的描述信息。当我们在工程中添加Web引用时,Visual Studio会生成一个代理类,用来提供Web服务的本地描述,从而允许客户端代码可以直接调用Web Service提供的功能。你可以通过代理类来调用Web Service提供的方法,代理类会负责在客户端应用程序和Web Service之间进行通信。下面是在Visual Studio中添加Web引用到Web Application的步骤:

  打开Solution Explorer,右键点击项目名称,在弹出的上下文菜单中选择Add Web Reference。

2010-7-19 14-47-46

  在弹出的对话框中输入http://api.search.live.net/search.wsdl,点击Go。

2010-7-19 14-59-15   如果连接成功,你应该会看到有一个服务“search”被找到,点击Add Rederence按钮添加该Web引用到工程中。

  浏览Solution Explorer面板,你会发现多了一个Web Reference文件夹,里面有live search service的API,双击它可以在Object Browser窗口中查看命名空间和对象。

  同时,Web.config文件也会被修改,其中包含了soap.asmx服务的引用地址,如:

<applicationSettings>
   
<BingSearchDemo.Properties.Settings>
       
<setting name="BingSearchDemo_net_live_search_api_LiveSearchService"
            serializeAs
="String">
           
<value>http://api.search.live.net:80/soap.asmx</value>
       
</setting>
   
</BingSearchDemo.Properties.Settings>
</applicationSettings>

 

使用Bing ServiceTypes进行搜索

  我们可以选择不同的Bing ServiceType进行在线搜索,下面是两种比较简单的应用类型:

  • Web:从Internet获取信息。
  • Phonebook:查找特定位置信息。

  如何选择ServiceType呢?在BingSearchDemo.net.live.search.api命名空间下可以找到SourceType枚举对象,它下面包含了所有可能的SourceType枚举类型,如Ad、Image、InstantAnswer、MoibleWeb、News、Phonebook、QueryLocation、RelatedSearch、Showtimes、Spell、Translation、Video、Weather、Web、XRank。下面有两个类,分别使用SourceType.Web类型和SourceType.Phonebook类型:

UsingWebCourceType
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using BingSearchDemo.net.live.search.api;

namespace BingSearchDemo
{
   
public class UsingWebSourceType
    {
       
// Replace the following string with the AppId you received from the
       
// Bing Developer Center.
        const string AppId = "0C6C67B56FCE6E3C401882E9A7BAE196062FF144";

       
public static SearchResponse PerformLiveSearch()
        {
           
// LiveSearchService implements IDisposable.
            using (LiveSearchService service = new LiveSearchService())
            {
                SearchResponse response
= new SearchResponse();
               
try
                {
                    SearchRequest request
= BuildRequest();
                   
// Send the request; display the response.
                    response = service.Search(request);
                }
               
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.
                    HttpContext.Current.Response.Write(ex.Message);
                }
               
return response;
            }
        }

       
public static SearchRequest BuildRequest()
        {
            SearchRequest request
= new SearchRequest();
           
// Common request fields (required)
            request.AppId = AppId;
            request.Query
= "msdn blogs";
            request.Sources
= new SourceType[] { SourceType.Web };
           
// 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 };
           
// Web-specific request fields (optional)
            request.Web = new WebRequest();
            request.Web.Count
= 10;
            request.Web.CountSpecified
= true;
            request.Web.Offset
= 0;
            request.Web.OffsetSpecified
= true;
            request.Web.Options
= new WebSearchOption[] { WebSearchOption.DisableHostCollapsing, WebSearchOption.DisableQueryAlterations };
           
return request;
        }

       
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.
                HttpContext.Current.Response.Write("Errors");
               
foreach (XmlNode error in errors)
                {
                   
foreach (XmlNode detail in error.ChildNodes)
                    {
                        HttpContext.Current.Response.Write(detail.Name
+ ": " + detail.InnerText);
                    }
                }
            }
        }
    }
}

 

UsingPhonebookSourceType
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BingSearchDemo.net.live.search.api;
using System.Xml;

namespace BingSearchDemo
{
   
public class UsingPhonebookSourceType
    {
       
// Replace the following string with the AppId you received from the
       
// Bing Developer Center.
        const string AppId = "0C6C67B56FCE6E3C401882E9A7BAE196062FF144";

       
public static SearchResponse PerformLiveSearch()
        {
           
// LiveSearchService implements IDisposable.
            using (LiveSearchService service = new LiveSearchService())
            {
                SearchResponse response
= new SearchResponse();
               
try
                {
                    SearchRequest request
= BuildRequest();
                   
// Send the request; display the response.
                    response = service.Search(request);
                }
               
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.
                    HttpContext.Current.Response.Write(ex.Message);
                }
               
return response;
            }
        }

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

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

           
// Common request fields (optional)
            request.Version = "2.0";
            request.Market
= "en-us";
            request.UILanguage
= "en";
            request.Latitude
= 47.603450;
            request.LatitudeSpecified
= true;
            request.Longitude
= -122.329696;
            request.LongitudeSpecified
= true;
            request.Radius
= 10.0;
            request.RadiusSpecified
= true;
            request.Options
= new SearchOption[] { SearchOption.EnableHighlighting };

           
// Phonebook-specific request fields (optional)
            request.Phonebook = new PhonebookRequest();
            request.Phonebook.Count
= 10;
            request.Phonebook.CountSpecified
= true;
            request.Phonebook.Offset
= 0;
            request.Phonebook.OffsetSpecified
= true;
            request.Phonebook.FileType
= "YP";
            request.Phonebook.SortBy
= PhonebookSortOption.Distance;
            request.Phonebook.SortBySpecified
= true;

           
return request;
        }

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

  代码中使用的AppId需要去Bing Developer Center申请(免费),上面的AppId是我在测试代码的时候申请的,应该可以使用。接下来我们需要在工程中创建两个Web Page来测试这两个类,下面是这两个页面的测试代码(HTML & C#): 

WebSourceType.aspx
<h2>
    Using the Web SourceType Over the SOAP Protocol
</h2>
This example shows how to perform the following tasks:
<ul>
   
<li>Set search request basic parameters by using the <a href="http://msdn.microsoft.com/en-us/library/dd250960.aspx"
        target
="_blank">SearchRequest</a> type.</li>
   
<li>Set the Web book request by using the <a href="http://msdn.microsoft.com/en-us/library/dd250886.aspx"
        target
="_blank">WebRequest</a> type. </li>
   
<li>Display the results obtained from the <a href="http://msdn.microsoft.com/en-us/library/dd250843.aspx"
        target
="_blank">SearchResponse</a> type. </li>
</ul>
<h4>
    See Also
</h4>
<span style="background-color: Yellow"><a href="http://msdn.microsoft.com/en-us/library/dd251056.aspx"
    target
="_blank">BING API</a></span>
<br />
<br />
<span style="background-color: Yellow">For more information, see <a href="http://blogs.msdn.com/morebits/"
    target
="_blank">Technical Notes</a></span>
<br />
<br />
<asp:Table ID="WebResultID" BorderWidth="1" runat="server">
   
<asp:TableHeaderRow BackColor="LightGray">
       
<asp:TableCell ID="hdrID1" BorderStyle="Inset" />
   
</asp:TableHeaderRow>
</asp:Table>

  

WebSourceType.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BingSearchDemo.net.live.search.api;

namespace BingSearchDemo
{
   
public partial class WebSourceType : System.Web.UI.Page
    {
       
protected void Page_Load(object sender, EventArgs e)
        {

        }

       
// Get the search results. Display one result per row.
        private void DisplayResults(SearchResponse response)
        {
           
int j = 0;
           
foreach (WebResult result in response.Web.Results)
            {
                TableRow tRow
= new TableRow();
                WebResultID.Rows.Add(tRow);
                TableCell tCell
= new TableCell();
                tCell.BorderWidth
= Unit.Parse("1");
               
if (j % 2 == 0)
                    tCell.BackColor
= System.Drawing.Color.Blue;
               
else
                    tCell.BackColor
= System.Drawing.Color.Tomato;
                tCell.ForeColor
= System.Drawing.Color.Yellow;
                tCell.Font.Bold
= true;
                System.Text.StringBuilder builder
= new System.Text.StringBuilder();
                builder.AppendLine(result.Title);
                builder.AppendLine(result.Description);
                builder.AppendLine(result.Url);
                builder.Append(
"Last Crawled: ");
                builder.AppendLine(result.DateTime);
                j
++;
               
int i = 0;
               
foreach (char c in builder.ToString().ToCharArray())
                {
                   
if (c == '\uE000')
                    {
                       
// If the current character is the begin highlighting
                       
// character (U+E000), change it to a left square bracket.
                        builder[i] = Convert.ToChar('[');
                    }
                   
else if (c == '\uE001')
                    {
                       
// If the current character is the end highlighting
                       
// character (U+E001), change it to a right square bracket.
                        builder[i] = Convert.ToChar(']');
                    }
                    i
++;
                }
                tCell.Text
= builder.ToString();
                tRow.Cells.Add(tCell);
            }
        }

       
protected override void OnPreRender(EventArgs e)
        {
           
base.OnPreRender(e);
            SearchResponse response
= UsingWebSourceType.PerformLiveSearch();
           
string results = string.Format("Displaying {0} to {1} of {2} results", response.Web.Offset + 1,
                           response.Web.Offset
+ response.Web.Results.Length, response.Web.Total);
           
// Add header information to the table.
            hdrID1.Text = "<div style='color:red; font-weight:bold'>Bing API Version: " + response.Version + "</div>" +
                           
"<div style='color:red; font-weight:bold'>Web results for " + response.Query.SearchTerms + "</div>" +
                           
"<div style='color:red; font-weight:bold'>" + results + "</div>";
           
// Add rows to the table that contain search results.
            DisplayResults(response);
        }
    }
}

  

PhonebookSourceType.aspx
<h2>
    Using the Web SourceType Over the SOAP Protocol
</h2>
This example shows how to perform the following tasks:
<ul>
   
<li>Set search request basic parameters by using the <a href="http://msdn.microsoft.com/en-us/library/dd250960.aspx"
        target
="_blank">SearchRequest</a> type.</li>
   
<li>Set the Web book request by using the <a href="http://msdn.microsoft.com/en-us/library/dd250886.aspx"
        target
="_blank">WebRequest</a> type. </li>
   
<li>Display the results obtained from the <a href="http://msdn.microsoft.com/en-us/library/dd250843.aspx"
        target
="_blank">SearchResponse</a> type. </li>
</ul>
<h4>
    See Also
</h4>
<span style="background-color: Yellow"><a href="http://msdn.microsoft.com/en-us/library/dd251056.aspx"
    target
="_blank">BING API</a></span>
<br />
<br />
<span style="background-color: Yellow">For more information, see <a href="http://blogs.msdn.com/morebits/"
    target
="_blank">Technical Notes</a></span>
<br />
<br />
<asp:Table ID="WebResultID" BorderWidth="1" runat="server">
   
<asp:TableHeaderRow BackColor="LightGray">
       
<asp:TableCell ID="hdrID1" BorderStyle="Inset" />
   
</asp:TableHeaderRow>
</asp:Table>

  

PhonebookSourceType.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BingSearchDemo.net.live.search.api;

namespace BingSearchDemo
{
   
public partial class PhonebookSourceType : System.Web.UI.Page
    {
       
protected void Page_Load(object sender, EventArgs e)
        {

        }

       
// Get the search results. Display one result per row.
        private void DisplayResults(SearchResponse response)
        {
           
int j = 0;
           
foreach (PhonebookResult result in response.Phonebook.Results)
            {
                TableRow tRow
= new TableRow();
                WebResultID.Rows.Add(tRow);
                TableCell tCell
= new TableCell();
                tCell.BorderWidth
= Unit.Parse("1");
               
if (j % 2 == 0)
                    tCell.BackColor
= System.Drawing.Color.Blue;
               
else
                    tCell.BackColor
= System.Drawing.Color.Tomato;
                tCell.ForeColor
= System.Drawing.Color.Yellow;
                tCell.Font.Bold
= true;
                System.Text.StringBuilder builder
= new System.Text.StringBuilder();
                builder.AppendLine(result.Business);
                builder.AppendLine(result.Address);
                builder.Append(result.City);
                builder.Append(
", ");
                builder.AppendLine(result.StateOrProvince);
                builder.AppendLine(result.PhoneNumber);
                builder.Append(
"Average Rating: ");
                builder.AppendLine(result.UserRating.ToString());

                j
++;
               
int i = 0;
               
foreach (char c in builder.ToString().ToCharArray())
                {
                   
if (c == '\uE000')
                    {
                       
// If the current character is the begin highlighting
                       
// character (U+E000), change it to a left square bracket.
                        builder[i] = Convert.ToChar('[');
                    }
                   
else if (c == '\uE001')
                    {
                       
// If the current character is the end highlighting
                       
// character (U+E001), change it to a right square bracket.
                        builder[i] = Convert.ToChar(']');
                    }
                    i
++;
                }
                tCell.Text
= builder.ToString();
                tRow.Cells.Add(tCell);
            }
        }

       
protected override void OnPreRender(EventArgs e)
        {
           
base.OnPreRender(e);
            SearchResponse response
= UsingPhonebookSourceType.PerformLiveSearch();
           
string results = string.Format("Displaying {0} to {1} of {2} results", response.Phonebook.Offset + 1,
                           response.Phonebook.Offset
+ response.Phonebook.Results.Length, response.Phonebook.Total);
           
// Add header information to the table.
            hdrID1.Text = "<div style='color:red; font-weight:bold'>Bing API Version: " + response.Version + "</div>" +
                           
"<div style='color:red; font-weight:bold'>Phonebook results for " + response.Query.SearchTerms + "</div>" +
                           
"<div style='color:red; font-weight:bold'>" + results + "</div>";
           
// Add rows to the table that contain search results.
            DisplayResults(response);
        }
    }
}

  下面是在我机器上使用上面两种类型执行查询的结果:

1

2

  Bing Search Service API的详细介绍和SDK文档可以去Bing Developer Center,本文给出的例子在这里下载。

原文地址

原文地址:https://www.cnblogs.com/jaxu/p/1780523.html