我想要使用新產品亞馬遜API來搜索亞馬遜上的產品。我一直在看他們的示例代碼和其他人的例子,但我沒有收到任何結果,並想知道是否有其他人最近使用過API並可以提供一些幫助?亞馬遜API,產品廣告API,ItemSearch,C#
using System;
using System.ServiceModel;
using Simple.Amazon.ECS;
namespace Simple {
class Program {
// your Amazon ID's
private const string accessKeyId = "*******************";
private const string secretKey = "************************************";
// the program starts here
static void Main(string[] args) {
// create a WCF Amazon ECS client
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
binding.MaxReceivedMessageSize = int.MaxValue;
AWSECommerceServicePortTypeClient client = new AWSECommerceServicePortTypeClient(
binding,
new EndpointAddress("https://webservices.amazon.com/onca/soap?Service=AWSECommerceService"));
// add authentication to the ECS client
client.ChannelFactory.Endpoint.Behaviors.Add(new AmazonSigningEndpointBehavior(accessKeyId, secretKey));
// prepare an ItemSearch request
ItemSearchRequest request = new ItemSearchRequest();
request.SearchIndex = "Books";
request.Title = "WCF";
request.ResponseGroup = new string[] { "Small" };
ItemSearch itemSearch = new ItemSearch();
itemSearch.Request = new ItemSearchRequest[] { request };
itemSearch.AWSAccessKeyId = accessKeyId;
// issue the ItemSearch request
ItemSearchResponse response = client.ItemSearch(itemSearch);
// write out the results
foreach (var item in response.Items[0].Item) {
Console.WriteLine(item.ItemAttributes.Title);
}
}
}
}
所有樣品/例子是在結構上與此類似,但是當涉及到foreach循環沒有項目返回(NULL),所以我得到一個空的異常錯誤。
「響應」究竟包含什麼內容? –
「未將對象引用設置爲對象的實例」。當它到達foreach循環。 – Standage
希望我能告訴你什麼是錯的。我運行了我從[1]下載的代碼,並按預期運行。 [1] http://flyingpies.wordpress。com/2009/08/01/17/ –