淘宝api例子 通过宝贝地址取宝贝标题价格图片

这里要加淘宝的api的dll文件“TopSdk.dll”,sdk里有

using Top.Api;
using Top.Api.Domain;
using Top.Api.Response;
using Top.Api.Request;


public static class taobao_message

        {
            //public string url = "http://gw.api.tbsandbox.com/router/rest";//沙箱环境调用地址,
            public static string url = "http://gw.api.taobao.com/router/rest";// 正式环境调用地址
            public static string appkey = "xxxxxx";//这个可以在淘宝开放平台上申请到的,偶就不说了
            public static string appsecret = "xxxxxxxxxx";//这个可以在淘宝开放平台上申请到的,偶就不说了
            /// <summary>
            /// 读取宝贝的信息
            /// string[]{标题,图片地址,价格}
            /// </summary>
            /// <param name="str_url"></param>
            /// <returns></returns>
            public static string[] baobei_mess(string str_url)
            {
                str_url = str_url.Replace("http://", "");

                long id = long.Parse(pipei(str_url, 1));//正则匹配url里面的宝贝id

                ITopClient client = new DefaultTopClient(url, appkey, appsecret);//连接初始化(TopSdk.dll)

                ItemGetRequest req = new ItemGetRequest();//初始化取宝贝信息的方法(TopSdk.dll)
                req.Fields = "title,pic_url,price";//要取的内容
                req.NumIid = id;//要读取的宝贝id
                ItemGetResponse response = client.Execute(req);//执行,通过api通讯要求返回指定的xml信息
                string title = response.Item.Title;
                string picurl = response.Item.PicUrl;
                string price = response.Item.Price;
                string[] arry1 = { title, picurl, price };
                return arry1;
            }
            public static string pipei(string content, int type)
            {
                string result = "";
                string reg = "";
                switch (type)
                {
                    case 1:
                        reg = @"[\?\&](item_id|id)\=([\d]+)"; break;//匹配宝贝id
                }
                Regex re = new Regex(reg);
                MatchCollection matches = re.Matches(content);
                System.Collections.IEnumerator enu = matches.GetEnumerator();
                while (enu.MoveNext() && enu.Current != null)
                {
                    Match match = (Match)(enu.Current);
                    result += match.Groups[1];
                }
                return result;
            }

        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值