博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net 淘宝api获取商品信息
阅读量:4646 次
发布时间:2019-06-09

本文共 3292 字,大约阅读时间需要 10 分钟。

之前做了一个项目,记录下对于我来说比较新的技术,通过淘宝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:为页面返回值

转载于:https://www.cnblogs.com/sing2550/archive/2012/11/16/GetTaoBaoInfo.html

你可能感兴趣的文章
C++ 头文件
查看>>
ZOJ 1008 Gnome Tetravex(DFS)
查看>>
Mysql基础知识:操作数据库
查看>>
mysql 数据库远程访问设置方法
查看>>
Far manager界面混乱问题解决
查看>>
java读取xml文件
查看>>
Go数组和切片定义和初始化
查看>>
用javascript将数据导入Excel
查看>>
域账号修改后,导致vs中的git连接失败
查看>>
redis集群结构图
查看>>
是用CXF开发Web Service
查看>>
python 作用域
查看>>
ajaxSetup()方法
查看>>
蓝桥杯 矩阵乘法 模板
查看>>
iOS_SourceTree忽略CocoaPods文件
查看>>
novoton-timer使用
查看>>
[Office]PPT 2013如何设置图片为半透明?
查看>>
返回顶部
查看>>
Log4cplus <1> 编译
查看>>
TaskTracker发送Heartbeat以及接受HeartbeatResponse
查看>>