環境:Visual Studio 2008, .NET Framework 3.5

 

Bing API の Webサイト 検索を SOAP から使ってみます。

Bing API を使用するためには AppID が必要となりますので、Bing Developer Center で取得します。

Bing API を使う(準備)
http://www.gine.jp/blog/taka/post/Bing-API-e38292e4bdbfe38186efbc88e6ba96e58299efbc89.aspx

 

Web 参照の追加 で Bing API Web サービスを追加します。

1.「Web 参照の追加」でURLに「http://api.search.live.net/search.wsdl」を入力し、移動をクリックします。

2.しばらくするとURLの下に「"LiveSearchService"の説明」が表示されますので、メゾット(Search)が表示されたのを確認し、右側の真ん中くらいにある 「参照の追加」 をクリックします。

1001

 

追加が終われば、普通のオブジェクトと同様に使用することが可能となります。下記は Bing API を使って Web サイト検索を行う asp.net の簡単なサンプルです。
※AppId部分は取得した AppID に変更が必要です。

 

default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="bing_default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Bing API で検索</title>
    <style type="text/css">
        p { margin:0 0 0.2em 0;padding:0;line-height:1.2em; }
        .title { font-weight:bold; }
        .description { font-size:smaller; }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <p><asp:TextBox ID="reqQuery" runat="server"></asp:TextBox><asp:Button ID="search" 
                runat="server" Text="検索" onclick="search_Click" /></p>
        <p><asp:Label ID="count" runat="server" Text=""></asp:Label></p>
        <br />
        <asp:DataList ID="results" runat="server">
        <ItemTemplate>
            <p class="title"><asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "Url")%>' ><%# DataBinder.Eval(Container.DataItem, "Title")%></asp:HyperLink></p>
            <p class="description"><%# DataBinder.Eval(Container.DataItem, "Description")%></p>
            <br />
        </ItemTemplate>
        </asp:DataList>
    </div>
    </form>
</body>
</html>

 

default.aspx.cs

using System;
using net.live.search.api;

public partial class bing_default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

    protected void search_Click(object sender, EventArgs e)
    {

        using (LiveSearchService liveSearch = new LiveSearchService())
        {
            SearchRequest request = new SearchRequest();
            request.AppId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            request.Query = reqQuery.Text;
            request.Sources = new SourceType[] { SourceType.Web };

            SearchResponse response = liveSearch.Search(request);

            if (response.Web != null)
            {
                count.Text = string.Format("{0} 件中 {1} - {2} 件",
                    string.Format("{0:N0}", response.Web.Total),
                    response.Web.Offset + 1,
                    response.Web.Offset + response.Web.Results.Length);

                results.DataSource = response.Web.Results;
                results.DataBind();
            }

        }
    }
}

 

結果

1002

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading