之前做了一个项目,记录下对于我来说比较新的技术,通过淘宝api获取商品信息。
不过,只能获取到一张图片,假如该商品有多张图片时,没法获取到,可以讨论下这个多张图片获取的。
View Code
注意要引用: 1 using System; 2 using System.Collections; 3 using System.Configuration; 4 using System.Data; 5 using System.Linq; 6 using System.Web; 7 using System.Web.Security; 8 using System.Web.UI; 9 using System.Web.UI.HtmlControls;10 using System.Web.UI.WebControls;11 using System.Web.UI.WebControls.WebParts;12 using System.Xml.Linq;13 using Top.Api;14 using Top.Api.Domain;15 using Top.Api.Response;16 using Top.Api.Request;17 using System.Text.RegularExpressions;18 19 namespace taobaoapi20 {21 public partial class wq : System.Web.UI.Page22 {23 public string purxx = ""; //页面返回值24 public static string url = "http://gw.api.taobao.com/router/rest"; // 正式环境调用地址25 public static string appkey = "21021937"; //淘宝开放平台上申请26 public static string appsecret = "0b0257c389f57578a9ed4f3c12917948"; //淘宝开放平台上申请27 28 protected void Page_Load(object sender, EventArgs e)29 {30 string[] ff = baobei_mess("http://detail.tmall.com/item.htm?spm=a1z10.3.0.109.NnZl9f&id=10935249260&");//需要得到产品信息的链接31 purxx += ff[0] + ",";32 purxx += ff[1] + ",";33 purxx += ff[2];34 }35 36 ///37 /// 读取宝贝的信息38 /// string[]{标题,图片地址,价格}39 /// 40 /// 41 ///42 public static string[] baobei_mess(string str_url)43 {44 str_url = str_url.Replace("http://", "");45 long id = long.Parse(pipei(str_url, 1));//正则匹配url里面的宝贝id46 ITopClient client = new DefaultTopClient(url, appkey, appsecret);//连接初始化(TopSdk.dll)47 ItemGetRequest req = new ItemGetRequest();//初始化取宝贝信息的方法(TopSdk.dll)48 req.Fields = "title,pic_url,price";//要取的内容,可以添加更多49 req.NumIid = id;//要读取的宝贝id50 ItemGetResponse response = client.Execute(req);//执行,通过api通讯要求返回指定的xml信息51 string title = response.Item.Title.ToString();//.Item.Title;52 string picurl = response.Item.PicUrl.ToString();53 string price = response.Item.Price.ToString();54 string[] arry1 = { title, picurl, price, };55 return arry1;56 }57 58 /// 59 /// 获取链接中的id60 /// 61 /// 62 /// 63 ///64 public static string pipei(string content, int type)65 {66 string result = "";67 string reg = "";68 switch (type)69 {70 case 1:71 reg = @"[\?\&](item_id|id)\=([\d]+)"; break;//匹配宝贝id72 }73 Regex re = new Regex(reg);74 MatchCollection matches = re.Matches(content);75 System.Collections.IEnumerator enu = matches.GetEnumerator();76 while (enu.MoveNext() && enu.Current != null)77 {78 Match match = (Match)(enu.Current);79 result += match.Groups[2];80 }81 return result;82 }83 84 }85 }
using Top.Api;
using Top.Api.Domain;using Top.Api.Response;using Top.Api.Request;引用dll:TopSdk.dll
purxx:为页面返回值