Yahoo!検索Webサービス 画像検索のサンプルコード(C#)

このサンプルを実行するには、xsd.exeにより「/c」オプションで作成されたクラスが必要です。このクラスを添付しておきます。

fileImageSearchResponse.cs 732件 [詳細]
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<%@ Page Language="C#" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        //URLを構築する
        string requestUrl =
            "http://api.search.yahoo.co.jp/ImageSearchService/V1/imageSearch";
        //アプリケーションIDを指定(必ず指定すること)
        requestUrl += "?appid=XXXXX";
        //UTF-8エンコード
        requestUrl += "&query=" + Server.UrlEncode(TextBox1.Text);
        //検索の種類
        if (searchType.SelectedValue != "" && searchType.SelectedValue != "all")
            requestUrl += "&type=" + searchType.SelectedValue;
        //表示件数
        if (resultsCount.SelectedValue != "" && resultsCount.SelectedValue != "10")
            requestUrl += "&results=" + resultsCount.SelectedValue;
        //開始位置
        //requestUrl += "&start=" + "1";
        //検索するファイルの種類
        if (fileFormat.SelectedValue != "" && fileFormat.SelectedValue != "any")
            requestUrl += "&format=" + fileFormat.SelectedValue;
        //カラーか白黒か
        if (imageColor.SelectedValue != "" && imageColor.SelectedValue != "any")
            requestUrl += "&coloration=" + imageColor.SelectedValue;
        //アダルトコンテンツを含める
        if (adultOk.Checked)
            requestUrl += "&adult_ok=1";
        //検索するドメインを指定する
        if (searchDomain.Text != "")
            requestUrl += "&site=" + Server.UrlEncode(searchDomain.Text);
 
        //HttpWebRequestの作成
        System.Net.HttpWebRequest request =
            (System.Net.HttpWebRequest)System.Net.WebRequest.Create(
            requestUrl);
        System.Net.HttpWebResponse response = null;
        Yahoo.jp.ImageSearchResponse.ResultSet res;
        try
        {
            //レスポンスの取得
            response = (System.Net.HttpWebResponse)request.GetResponse();
            System.IO.Stream strm = response.GetResponseStream();
            //サーバーから返されたXMLデータを解析
            System.Xml.Serialization.XmlSerializer serializer =
                new System.Xml.Serialization.XmlSerializer(
                typeof(Yahoo.jp.ImageSearchResponse.ResultSet));
            res = (Yahoo.jp.ImageSearchResponse.ResultSet)
                serializer.Deserialize(strm);
        }
        catch (Exception ex)
        {
            Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message);
            return;
        }
        finally
        {
            if (response != null)
                response.Close();
        }
 
        //結果表示
        Label1.Text = "";
        if (res.totalResultsReturned == "0")
        {
            Label1.Text += "該当するページが見つかりませんでした。";
        }
        else
        {
            Label1.Text += "<b>" + Server.HtmlEncode(TextBox1.Text) +
                "</b> の検索結果 約 " +
                res.totalResultsAvailable + "件中 " +
                res.firstResultPosition + " - " +
                res.totalResultsReturned + " 件目<br />\n";
 
            Label1.Text += "<ul>\n";
            foreach (Yahoo.jp.ImageSearchResponse.ResultType item in res.Result)
            {
                //リンクの作成
                Label1.Text += "<li><a href=\"" + item.ClickUrl + "\">";
                //サムネイル画像の表示
                if (item.Thumbnail != null)
                    Label1.Text += "<img src=\"" + item.Thumbnail.Url + "\"><br />";
                //タイトル
                Label1.Text += "<b>" + item.Title + "</b></a>";
                //画像のサイズ
                Label1.Text += "<br />" + item.Width + "x" + item.Height +
                    "・" + item.FileSize + "byte";
                //画像のあるページへのリンク
                Label1.Text += "<br /><a href=\"" + item.RefererUrl +
                    "\">画像元ページへ</a>";
                //サマリー
                if (item.Summary != null && item.Summary.Length > 0)
                    Label1.Text += "<br />" + item.Summary;
                //制限事項
                if (item.Restrictions != null && item.Restrictions.Length > 0)
                    Label1.Text += "<br />" + item.Restrictions;
            }
            Label1.Text += "</ul>\n";
        }
    }
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Yahoo!画像検索Webサービスのサンプル</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        キーワード :
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
         Text="画像検索" /><br />
        キーワードを<asp:CheckBoxList ID="searchType" runat="server"
         RepeatDirection="Horizontal"
            RepeatLayout="Flow">
            <asp:ListItem Selected="True" Value="all">すべて含む
            </asp:ListItem>
            <asp:ListItem Value="any">少なくとも1つ含む</asp:ListItem>
            <asp:ListItem Value="phrase">文章として含む</asp:ListItem>
        </asp:CheckBoxList><br />
        表示件数 :
        <asp:DropDownList ID="resultsCount" runat="server">
            <asp:ListItem Value="10">10件</asp:ListItem>
            <asp:ListItem Value="25">25件</asp:ListItem>
            <asp:ListItem Value="50">50件</asp:ListItem>
        </asp:DropDownList><br />
        ファイル形式 :
        <asp:DropDownList ID="fileFormat" runat="server">
            <asp:ListItem Value="any">すべてのファイル形式</asp:ListItem>
            <asp:ListItem>bmp</asp:ListItem>
            <asp:ListItem>gif</asp:ListItem>
            <asp:ListItem>jpeg</asp:ListItem>
            <asp:ListItem>png</asp:ListItem>
        </asp:DropDownList><br />
        画像の色 :
        <asp:CheckBoxList ID="imageColor" runat="server"
         RepeatDirection="Horizontal" RepeatLayout="Flow">
            <asp:ListItem Selected="True" Value="any">すべて</asp:ListItem>
            <asp:ListItem Value="color">カラー</asp:ListItem>
            <asp:ListItem Value="bw">白黒・グレースケール</asp:ListItem>
        </asp:CheckBoxList><br />
        <asp:CheckBox ID="adultOk" runat="server"
         Text="アダルトコンテンツを含める" /><br />
        ドメイン :
        <asp:TextBox ID="searchDomain" runat="server"></asp:TextBox><br />
        <br />
        <asp:Label ID="Label1" runat="server" EnableViewState="False">
        </asp:Label>
        <span style="color: #0000ff; text-decoration: underline">
        </span>
 
<!-- Begin Yahoo! JAPAN Web Services Attribution Snippet -->
<span style="margin:15px 15px 15px 15px">
<a href="http://developer.yahoo.co.jp/about">Webサービス by Yahoo! JAPAN</a>
</span>
<!-- End Yahoo! JAPAN Web Services Attribution Snippet -->
    </div>
    </form>
</body>
</html>

ページ情報
  • 作成日 : 2006-09-12 (火) 02:51:50
  • 作成者 : DOBON!
  • 最終編集日 : 2006-09-12 (火) 02:51:50
  • 最終編集者 : DOBON!
[ トップ ]   [ 編集 | 凍結 | 差分 | バックアップ | 添付 | 複製 | 名前変更 | リロード ]   [ 新規 | 子ページ作成 | 一覧 | 単語検索 | 最終更新 | ヘルプ ]