VB.NET + Live Search API その2

投稿日 : 2007年12月9日 22:31

 その1からの続き。

検索するコード

 自動生成されたクラスを利用するのに名前空間が長いのでForm1のコードの先頭に以下のようにImportsしておきます。LiveSearchSampleはアプリケーションの名前空間です。

Imports LiveSearchSample.com.msn.search.soap

 そして、ボタンを押したとき初音ミクを検索してPictureBoxに表示させるコードは以下のようになります。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try

        Dim service As New MSNSearchService
        Dim sourceRequest As New SourceRequest
        Dim searchRequest As New SearchRequest

        sourceRequest.Source = SourceType.Image ' 画像を検索
        sourceRequest.ResultFields = ResultFieldMask.Image ' 検索結果に画像情報を指定
        sourceRequest.Count = 1 ' 1件検索

        searchRequest.Query = "初音ミク" ' 検索文字列
        searchRequest.AppID = "あなたのAppID" ' Application ID
        searchRequest.CultureInfo = "ja-JP" ' 日本語のページから検索(画像検索ではたぶん意味なし)
        searchRequest.Requests = New SourceRequest() {sourceRequest}

        ' 検索
        Dim searchResponse As SearchResponse = service.Search(searchRequest)

        ' 結果表示
        PictureBox1.ImageLocation = searchResponse.Responses(0).Results(0).Image.ImageURL

    Catch soapEx As System.Web.Services.Protocols.SoapException
        MessageBox.Show(soapEx.ToString)

    Catch webEx As System.Net.WebException
        MessageBox.Show(webEx.ToString)

    Catch ex As Exception
        MessageBox.Show(ex.ToString)

    End Try
End Sub

 MSNSearchServiceクラスのSearchメソッドを呼ぶことで検索ができます(18行目)。Searchメソッドの引数はSearchRequestクラスのオブジェクトです。SearchRequestには、SourceRequestクラスオブジェクトが必要になります。さーちリクエストとそーすリクスト、ややこしい……。

 SourceRequestには、取得する結果件数・検索するファイルタイプ・取得する結果のオフセット値・取得するフィールド(タイトルやURL、説明など)・検索するソース(Webページや画像など)などの設定を指定します。上記コードでは検索するソースに画像を指定(8行目)、取得するフィールドに画像情報を指定(9行目)、1件のみ検索結果を返すようにしています(10行目)。もしWebページから検索する場合は以下のようになります。

sourceRequest.Source = SourceType.Web
sourceRequest.ResultFields = ResultFieldMask.Title Or ResultFieldMask.Description Or ResultFieldMask.Url

 検索件数はデフォルトで10件まで、最高で50件を指定できます(SourceTypeがPhoneBookの場合は25です。PhoneBookは日本でサービスはしていません)。上記のResultFieldsにはタイトル・説明・URLを指定していますが、これがResultFieldsのデフォルト値になります。また、指定しても必ずその情報が返ってくるとは限りませんのでEmptyかどうかのチェックが必要です。

 SearchRequestには、Application ID・カルチャ情報・検索文字列・SourceRequest(の配列)を指定します。オプションでフィルタ・地域・その他の細かい検索の設定の指定ができます。

 Searchメソッドの戻り値は、SearchResponseクラスのオブジェクトになります。SearchResponseはSourceResponseクラスの配列をResponsesプロパティを持ち、各SourceResponseは、Resultクラスの配列をResultsプロパティに持っています。さーちレスポンスとそーすレスポンス、またややこしい。

 21行目ではPictureBox1のImageLocationプロパティに検索結果のURLを指定しています。結果がない場合もあるので本来ならばきちんとチェックしてあげる必要があります。また複数の結果を取得する方が良くあると思いますのでループ処理による検索結果の表示例も以下に書いておきます(expand sourceをクリックしてください)。

' Sample
For Each sourceResponse As SourceResponse In searchResponse.Responses
    Console.WriteLine(String.Format("検索結果 合計{0}件", sourceResponse.Total))

    For Each result As Result In sourceResponse.Results

        If Not String.IsNullOrEmpty(result.Title) Then
            Console.WriteLine(result.Title)
        End If
        If Not String.IsNullOrEmpty(result.Description) Then
            Console.WriteLine(result.Description)
        End If
        If Not String.IsNullOrEmpty(result.Url) Then
            Console.WriteLine(result.Url)
        End If
    Next
Next

結果

 さて実行結果は……ミクきたー。これを書いた時点では、http://zeloma.com/tag/%8F%89%89%B9%83~%83Nにある画像みたい。

実行結果

 ちなみに、Image.ImageURLではなくImage.ThumbnailURLを指定するとLive Searchが持っているサムネイル画像が表示されます。サムネイルは最大で160x160ピクセルのようですね。また、検索文字列を「初音ミク imagesize:DIM_H_1024」のように指定すると壁紙サイズ(この場合の高さが1024ピクセル)の画像が検索できます。

More Infomation

 Live Search APIの各クラスやプロパティなどについては細かく触れませんでしたのでより詳細な情報はSDKを見てください。英語ですが数も少ないし検索という簡単な対象なのでわかりやすいかと思います。コードサンプルもあります(C#)。また、Live Search Interactive SDKでは、実際に動作しているサンプルが見れ、C#のコードと実際に送信されるSOAPのデータ(XML形式)も確認できます。

 今回作成したプログラムで実際に送受信されていたデータは次のようなものでした。

送信時:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <Search xmlns="http://schemas.microsoft.com/MSNSearch/2005/09/fex">
      <Request>
        <AppID>あなたのAppID</AppID>
        <Query></Query>
        <CultureInfo>ja-JP</CultureInfo>
        <SafeSearch>Moderate</SafeSearch>
        <Flags>None</Flags>
        <Requests>
          <SourceRequest>
            <Source>Image</Source>
            <Offset>0</Offset>
            <Count>1</Count>
            <ResultFields>Image</ResultFields>
          </SourceRequest>
        </Requests>
      </Request>
    </Search>
  </soap:Body>
</soap:Envelope>

受信時:

<?xml version="1.0" encoding="utf-8" ?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <SearchResponse xmlns="http://schemas.microsoft.com/MSNSearch/2005/09/fex">
      <Response>
        <Responses>
          <SourceResponse>
            <Source>Image</Source>
            <Offset>0</Offset>
            <Total>4454</Total>
            <Results>
              <Result>
                <Image>
                  <ImageURL>http://ktaianime.up.seesaa.net/image/miku13439.jpg</ImageURL>
                  <ImageWidth>240</ImageWidth>
                  <ImageHeight>400</ImageHeight>
                  <ImageFileSize>30688</ImageFileSize>
                  <ThumbnailURL>http://t2.images.live.com/images/thumbnail.aspx?q=1431982184337&amp;id=b9ff1da72be70581941668e8f4e0849d</ThumbnailURL>
                  <ThumbnailWidth>96</ThumbnailWidth>
                  <ThumbnailHeight>160</ThumbnailHeight>
                  <ThumbnailFileSize>3751</ThumbnailFileSize>
                </Image>
              </Result>
            </Results>
          </SourceResponse>
        </Responses>
      </Response>
    </SearchResponse>
  </soapenv:Body>
</soapenv:Envelope>

フィードバック

# TJKisrQCMOf

2011/09/29 0:46 by http://oemfinder.com
aXEnhS Yeah? I read and I understand that I do not understand anything what it is about:D

# MiOSPVAywhxWcsK

2011/11/02 6:21 by http://optclinic.com/
I would add something else, of course, but in fact almost everything is mentioned!...

# QrGnzodShKuxpOa

2011/11/09 6:41 by http://www.farmaciaunica.com/
Thanks for all the answers:) In fact, learned a lot of new information. Dut I just didn`t figure out what is what till the end!...

# QOdeBLDbuwHXit

2011/11/16 2:58 by http://circalighting.com/lights.aspx
Good! Wish everybody wrote so:D

# aGfnqtzUfVRGLiyW

2011/11/16 3:37 by http://catalinabiosolutions.com/
This article is for professionals..!

# rlpvSELfozwdUwoBq

2011/11/16 3:58 by http://www.laurenslinens.com/boysbedding.html
Fresh thoughts, fresh view on the subject..!

# zTzGaeuuXNnaAYGlXj

2021/07/03 3:00 by https://amzn.to/365xyVY
you have brought up a very excellent details , thanks for the post.

# I love what you guys are usually up too. This sort of clever work and reporting! Keep up the superb works guys I've you guys to blogroll.

2021/09/01 0:43 by I love what you guys are usually up too. This sort
I love what you guys are usually up too. This sort of clever
work and reporting! Keep up the superb works guys I've you guys to
blogroll.

# Hi there! Someone in my Myspace group shared this website with us so I came to check it out. I'm definitely enjoying the information. I'm bookmarking and will be tweeting this to my followers! Terrific blog and wonderful design.

2021/09/02 17:16 by Hi there! Someone in my Myspace group shared this
Hi there! Someone in my Myspace group shared this website with us so I came to check it out.

I'm definitely enjoying the information. I'm bookmarking
and will be tweeting this to my followers! Terrific blog and wonderful design.

# Hi there! Someone in my Myspace group shared this website with us so I came to check it out. I'm definitely enjoying the information. I'm bookmarking and will be tweeting this to my followers! Terrific blog and wonderful design.

2021/09/02 17:17 by Hi there! Someone in my Myspace group shared this
Hi there! Someone in my Myspace group shared this website with us so I came to check it out.

I'm definitely enjoying the information. I'm bookmarking
and will be tweeting this to my followers! Terrific blog and wonderful design.

# Hi there! Someone in my Myspace group shared this website with us so I came to check it out. I'm definitely enjoying the information. I'm bookmarking and will be tweeting this to my followers! Terrific blog and wonderful design.

2021/09/02 17:18 by Hi there! Someone in my Myspace group shared this
Hi there! Someone in my Myspace group shared this website with us so I came to check it out.

I'm definitely enjoying the information. I'm bookmarking
and will be tweeting this to my followers! Terrific blog and wonderful design.

# Hi there! Someone in my Myspace group shared this website with us so I came to check it out. I'm definitely enjoying the information. I'm bookmarking and will be tweeting this to my followers! Terrific blog and wonderful design.

2021/09/02 17:19 by Hi there! Someone in my Myspace group shared this
Hi there! Someone in my Myspace group shared this website with us so I came to check it out.

I'm definitely enjoying the information. I'm bookmarking
and will be tweeting this to my followers! Terrific blog and wonderful design.

# I'm gone to tell my little brother, that he should also visit this web site on regular basis to obtain updated from newest news update.

2021/09/05 17:24 by I'm gone to tell my little brother, that he should
I'm gone to tell my little brother, that he should also visit this
web site on regular basis to obtain updated from newest news update.

# I'm gone to tell my little brother, that he should also visit this web site on regular basis to obtain updated from newest news update.

2021/09/05 17:25 by I'm gone to tell my little brother, that he should
I'm gone to tell my little brother, that he should also visit this
web site on regular basis to obtain updated from newest news update.

# I got this web page from my friend who told me about this website and at the moment this time I am browsing this web page and reading very informative content here. ps4 https://j.mp/3z5HwTp ps4

2021/09/15 7:29 by I got this web page from my friend who told me abo
I got this web page from my friend who told me
about this website and at the moment this time I am browsing this web page and reading very informative content here.
ps4 https://j.mp/3z5HwTp ps4

# I got this web page from my friend who told me about this website and at the moment this time I am browsing this web page and reading very informative content here. ps4 https://j.mp/3z5HwTp ps4

2021/09/15 7:30 by I got this web page from my friend who told me abo
I got this web page from my friend who told me
about this website and at the moment this time I am browsing this web page and reading very informative content here.
ps4 https://j.mp/3z5HwTp ps4

# I got this web page from my friend who told me about this website and at the moment this time I am browsing this web page and reading very informative content here. ps4 https://j.mp/3z5HwTp ps4

2021/09/15 7:31 by I got this web page from my friend who told me abo
I got this web page from my friend who told me
about this website and at the moment this time I am browsing this web page and reading very informative content here.
ps4 https://j.mp/3z5HwTp ps4

# I got this web page from my friend who told me about this website and at the moment this time I am browsing this web page and reading very informative content here. ps4 https://j.mp/3z5HwTp ps4

2021/09/15 7:32 by I got this web page from my friend who told me abo
I got this web page from my friend who told me
about this website and at the moment this time I am browsing this web page and reading very informative content here.
ps4 https://j.mp/3z5HwTp ps4

# Superb blog you have here but I was curious about if you knew of any message boards that cover the same topics talked about in this article? I'd really like to be a part of group where I can get suggestions from other experienced people that share the

2021/10/25 19:28 by Superb blog you have here but I was curious about
Superb blog you have here but I was curious about if you knew of any message boards that
cover the same topics talked about in this article?

I'd really like to be a part of group where I can get suggestions from other experienced
people that share the same interest. If you
have any suggestions, please let me know.
Many thanks!

# Superb blog you have here but I was curious about if you knew of any message boards that cover the same topics talked about in this article? I'd really like to be a part of group where I can get suggestions from other experienced people that share the

2021/10/25 19:29 by Superb blog you have here but I was curious about
Superb blog you have here but I was curious about if you knew of any message boards that
cover the same topics talked about in this article?

I'd really like to be a part of group where I can get suggestions from other experienced
people that share the same interest. If you
have any suggestions, please let me know.
Many thanks!

# Superb blog you have here but I was curious about if you knew of any message boards that cover the same topics talked about in this article? I'd really like to be a part of group where I can get suggestions from other experienced people that share the

2021/10/25 19:30 by Superb blog you have here but I was curious about
Superb blog you have here but I was curious about if you knew of any message boards that
cover the same topics talked about in this article?

I'd really like to be a part of group where I can get suggestions from other experienced
people that share the same interest. If you
have any suggestions, please let me know.
Many thanks!

# Superb blog you have here but I was curious about if you knew of any message boards that cover the same topics talked about in this article? I'd really like to be a part of group where I can get suggestions from other experienced people that share the

2021/10/25 19:31 by Superb blog you have here but I was curious about
Superb blog you have here but I was curious about if you knew of any message boards that
cover the same topics talked about in this article?

I'd really like to be a part of group where I can get suggestions from other experienced
people that share the same interest. If you
have any suggestions, please let me know.
Many thanks!

# It's very easy to find out any matter on net as compared to books, as I found this paragraph at this web site.

2021/11/14 19:40 by It's very easy to find out any matter on net as co
It's very easy to find out any matter on net as compared to books, as I found this paragraph at this web site.

# erection pills online https://erectionpills.best/
natural remedies for ed

2022/06/28 19:55 by ErectionPills
erection pills online https://erectionpills.best/
natural remedies for ed

# paxlovid pfizer https://paxlovid.best/
paxlovid side effects

2022/09/08 7:41 by Paxlovid
paxlovid pfizer https://paxlovid.best/
paxlovid side effects

# ed pills gnc https://erectiledysfunctionpills.shop/

2022/10/14 22:49 by Erectile
ed pills gnc https://erectiledysfunctionpills.shop/

# faroedating chat https://datingsiteonline.site/
dating sites free chatting

2022/12/05 23:49 by Tading
faroedating chat https://datingsiteonline.site/
dating sites free chatting

# dating sites https://datingonlinehot.com/
local free personal ads

2022/12/09 19:21 by Dating
dating sites https://datingonlinehot.com/
local free personal ads

# pills erectile dysfunction https://edpills.science/
ed treatment review

2023/01/07 13:51 by EdPills
pills erectile dysfunction https://edpills.science/
ed treatment review

# mexican pharmacies https://fastpills.pro/# canadian pharmaceuticals

2023/06/29 17:45 by FastPills
mexican pharmacies https://fastpills.pro/# canadian pharmaceuticals

# pills erectile dysfunction https://edpills.tech/# best medication for ed

2023/12/23 4:42 by EdPills
pills erectile dysfunction https://edpills.tech/# best medication for ed
コメントの入力
タイトル
名前
Url
コメント