这段代码演示了如何从eBay Catalog中按Title匹配来寻找一个产品。代码调用了Shopping API :FindProducts。对于可能出现的多个匹配产品,默认只返回前三个结果。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Xml;
namespace eBayProductSearch
{
public class eBayProdSearch
{
private void searcgProdByTitle
{
string Title = "LG 42" Plasma TV 42PQ3000"
string AppID = "YourAppID";
string error;
//make the FindProducts call
string request = "http://open.api.ebay.com/shopping?appid=" + AppID + "&version=623&siteid=0&callname=FindProducts&MaxEntries=3&QueryKeywords=" + Title;
WebRequest wrGETURL;
wrGETURL = WebRequest.Create(request);
Stream objStream;
try
{
//read the response
objStream = wrGETURL.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader(objStream);
string strResponse = objReader.ReadToEnd();
//parse the response
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(strResponse);
XmlNode root = xmlDoc["FindProductsResponse"];
string ProdIDs;
if (root["Errors"] != null)
{
//There is an error
error = root["Errors"]["ShortMessage"].InnerText;
Console.WriteLine(Title + "t" + error);
}
else
{
//Browse the response for Product matches
XmlNodeList ProdList = xmlDoc.GetElementsByTagName("Product");
//There could be more than one Product match
for (int i = 0; i < ProdList.Count; i++)
{
ProdIDs = "";
//Each Product can have multiple Product identifiers associated.
//We want to get all
XmlNodeList ProdIDList = ProdList[i].ChildNodes;
for (int j = 0; j < ProdIDList.Count; j++)
{
if (ProdIDList[j].Name == "ProductID")
{
XmlAttributeCollection attCol = ProdIDList[j].Attributes;
ProdIDs += attCol[0].Value + "t" + ProdIDList[j].InnerText + "t";
}
}
ProdIDs = ProdIDs.Replace("Reference", "ePID");
Console.WriteLine(Title + "t" + ProdIDs);
}
}
}
catch (Exception ex)
{
Console.WriteLine("FindProducts error - identifier = " + Title + "t" + ex.Message);
}
}
}
}
|