• 追加された行はこの色です。
  • 削除された行はこの色です。
*ItemSearchのサンプルコード(C#) [#q7b0ea10]
*Amazon E-Commerce Service - C#のサンプルコード [#kc97aeec]

Amazon.co.jpで商品を検索するサンプルです。TextBoxに検索するキーワードを入れて、DropDownListで検索するカテゴリを指定できます。さらに、BrowseNodeを入力することもできるようになっています。
ここで紹介しているコードは[[こちらのプロキシクラス>../../ProxyClass]]を使用しています。

#code(csharp){{
<%@ Page Language="C#" %>
<%@ Import Namespace="Amazon.jp.v20060913" %>
#lsx

<!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)
    {
        ItemSearchRequest req = new ItemSearchRequest();
        //検索するストアのインデックスを指定する(必須)
        req.SearchIndex = searchIndex.SelectedValue;
        //検索する語句を指定
        req.Keywords = TextBox1.Text;
        //取得するデータの種類(範囲)を指定 
        req.ResponseGroup = new string[] {
            "Small","ItemAttributes", "OfferFull",
            "Images", "Reviews", "EditorialReview",
            "ListmaniaLists", "Similarities", "Tracks"};
        //ページを指定する。1-400まで。1ページ10の結果まで取得。
        req.ItemPage = "1";
        //並び替え方
        //SearchIndexや地域によって異なる
        if (req.SearchIndex != "Blended" &&
            req.SearchIndex != "MusicTracks")
            req.Sort = "salesrank";
        //BrowseNode
        if (TextBox2.Text != "")
            req.BrowseNode = TextBox2.Text;
        //新品以外や、Amazon以外の売り手の商品も表示
        if (req.SearchIndex != "Blended" &&
            req.SearchIndex != "MusicTracks")
        {
            req.Condition = Condition.All;
            req.ConditionSpecified = true;
            req.MerchantId = "All";
            //利用可能な商品のみ表示
            req.Availability = ItemSearchRequestAvailability.Available;
            req.AvailabilitySpecified = true;
        }

        ItemSearch isc = new ItemSearch();
        //あなたのAccess Key ID
        //AWSAccessKeyIdかSubscriptionIdのどちらかを必ず指定する
        isc.AWSAccessKeyId = "(あなたのAccessKeyID)";
        //あなたのAssociate ID
        isc.AssociateTag = "dobonnet-22";
        isc.Request = new ItemSearchRequest[] { req };

        AWSECommerceService aws = new AWSECommerceService();
        //プロキシを通す場合は、次のようにする
        //aws.Proxy = new System.Net.WebProxy("localhost", 8080);
        ItemSearchResponse res;
        try
        {
            //ItemSearchオペレーションを実行し、結果を取得
            res = aws.ItemSearch(isc);
        }
        catch (Exception ex)
        {
            Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message);
            return;
        }

        if (res == null || res.Items == null)
        {
            Label1.Text = "結果を取得できませんでした。";
            return;
        }
        if (res.Items[0].Request.Errors != null)
        {
            Label1.Text = "エラー:" +
                res.Items[0].Request.Errors[0].Message;
            return;
        }
        if (res.Items[0].Item == null)
        {
            Label1.Text = "該当する商品が見つかりませんでした。";
            return;
        }

        //結果を表示
        Label1.Text = "";
        Label1.Text += res.Items[0].TotalResults +
            " 件見つかりました。<br />\n";
        Label1.Text += "<ul>\n";
        foreach (Item i in res.Items[0].Item)
        {
            //タイトル
            Label1.Text += "<li><b><a href=\"" + i.DetailPageURL + "\">" +
                i.ItemAttributes.Title + "</a></b><br />\n";
            Label1.Text += "ASIN : " + i.ASIN + "<br />\n";

            //画像を表示
            //ResponseGroupにImagesなどが必要
            if (i.MediumImage != null)
                Label1.Text += "<img src=\"" + i.MediumImage.URL +
                    "\" width=\"" + i.MediumImage.Width.Value.ToString() +
                    "\" height=\"" + i.MediumImage.Height.Value.ToString() +
                    "\"><br />\n";

            //クリエーター情報
            if (i.ItemAttributes.Creator != null)
            {
                foreach (ItemAttributesCreator c in i.ItemAttributes.Creator)
                {
                    Label1.Text += c.Role + " : " + c.Value + "<br />\n";
                }
            }
            //出版社
            if (i.ItemAttributes.Publisher != null)
                Label1.Text += i.ItemAttributes.Publisher + "<br />\n";
            
            //価格
            //ResponseGroupにItemAttributesなどが必要
            if (i.ItemAttributes.ListPrice != null)
                Label1.Text += "参考価格 : " +
                    i.ItemAttributes.ListPrice.FormattedPrice + "<br />\n";
            //ResponseGroupにOfferFullなどが必要
            //実際の価格の表示
            if (i.Offers != null && i.Offers.Offer != null)
            {
                Label1.Text += "価格 : <br />\n";
                Label1.Text += "<ul>\n";
                foreach (Offer off in i.Offers.Offer)
                {
                    if (off.OfferListing != null)
                    {
                        Label1.Text += "<ul>\n";
                        foreach (OfferListing ol in off.OfferListing)
                        {
                            Label1.Text +=
                                "<li>" + ol.Price.FormattedPrice + "\n";
                        }
                        Label1.Text += "</ul>\n";
                    }
                }
                Label1.Text += "</ul>\n";
            }
            
            //最安値
            if (i.OfferSummary != null &&
                i.OfferSummary.LowestNewPrice != null &&
                i.OfferSummary.LowestNewPrice.FormattedPrice != null)
                Label1.Text += "新品の最安値 : " +
                    i.OfferSummary.LowestNewPrice.FormattedPrice +
                    "<br />\n";
            
            //EditorialReview
            if (i.EditorialReviews != null)
            {
                Label1.Text += "エディターレビュー : <br />";
                Label1.Text += "<ul>\n";
                foreach (EditorialReview r in i.EditorialReviews)
                {
                    Label1.Text += "<li>" + r.Source + " : "
                        + r.Content + "\n";
                }
                Label1.Text += "</ul>\n";
            }
            
            //カスタマーレビュー
            if (i.CustomerReviews != null)
            {
                Label1.Text += "お勧め度 : " +
                    i.CustomerReviews.AverageRating.ToString() +
                    "<br />";
                Label1.Text += "カスタマーレビュー : <br />";
                Label1.Text += "<ul>\n";
                foreach (Review r in i.CustomerReviews.Review)
                {
                    Label1.Text += "<li>" + r.Summary + "\n";
                }
                Label1.Text += "</ul>\n";
            }
            
            //リストマニア
            if (i.ListmaniaLists != null)
            {
                Label1.Text += "リストマニア : <br />";
                Label1.Text += "<ul>\n";
                foreach (ListmaniaListsListmaniaList lm in i.ListmaniaLists)
                {
                    Label1.Text += "<li>" + lm.ListName + "\n";
                }
                Label1.Text += "</ul>\n";
            }
            
            //この商品を買った人はこんな商品も買っています
            if (i.SimilarProducts != null)
            {
                Label1.Text += "この商品を買った人はこんな商品も買っています" +
                    " : <br />";
                Label1.Text += "<ul>\n";
                foreach (SimilarProductsSimilarProduct sp in i.SimilarProducts)
                {
                    Label1.Text += "<li>" + sp.Title + "\n";
                }
                Label1.Text += "</ul>\n";
            }
            
            //曲目リスト
            if (i.Tracks != null)
            {
                Label1.Text += "曲目リスト : <br />";
                Label1.Text += "<ul>\n";
                foreach (TracksDisc td in i.Tracks)
                {
                    Label1.Text += "<li>" + td.Number + "\n";
                    if (td.Track != null)
                    {
                        Label1.Text += "<ul>\n";
                        foreach (TracksDiscTrack tdt in td.Track)
                            Label1.Text +=
                                "<li>" + tdt.Number + ":" + tdt.Value + "\n";
                        Label1.Text += "</ul>\n";
                    }
                }
                Label1.Text += "</ul>\n";
            }
        }
        Label1.Text += "</ul>\n";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Amazon商品検索</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="検索" EnableViewState="False" /><br />
        <asp:DropDownList ID="searchIndex" runat="server">
            <asp:ListItem Value="Blended">すべて</asp:ListItem>
            <asp:ListItem Value="Books">本</asp:ListItem>
            <asp:ListItem Value="Classical">クラシック</asp:ListItem>
            <asp:ListItem>DVD</asp:ListItem>
            <asp:ListItem Value="Electronics">エレクトロニクス</asp:ListItem>
            <asp:ListItem Value="ForeignBooks">洋書</asp:ListItem>
            <asp:ListItem Value="Hobbies">ホビー</asp:ListItem>
            <asp:ListItem Value="Kitchen">ホーム&キッチン</asp:ListItem>
            <asp:ListItem Value="Music">音楽</asp:ListItem>
            <asp:ListItem Value="MusicTracks">曲名</asp:ListItem>
            <asp:ListItem Value="Software">ソフトウェア</asp:ListItem>
            <asp:ListItem Value="SportingGoods">スポーツ</asp:ListItem>
            <asp:ListItem Value="Toys">おもちゃ</asp:ListItem>
            <asp:ListItem>VHS</asp:ListItem>
            <asp:ListItem Value="Video">DVD&ビデオ</asp:ListItem>
            <asp:ListItem Value="VideoGames">ゲーム</asp:ListItem>
        </asp:DropDownList><br />
        <label for="TextBox2">BrowseNode : </label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        </div>
    </form>
    <br />
    <asp:Label ID="Label1" runat="server" EnableViewState="False">
    </asp:Label>
</body>
</html>
}}

//これより下は編集しないでください
#pageinfo(,2006-10-20 (金) 00:21:32,DOBON!,2006-10-20 (金) 00:21:32,DOBON!)
#pageinfo(,2006-10-20 (金) 00:21:32,DOBON!,2006-10-20 (金) 02:05:48,DOBON!)


[ トップ ]   [ 新規 | 子ページ作成 | 一覧 | 単語検索 | 最終更新 | ヘルプ ]