您的位置:首页 > 其它

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

2013-01-06 15:50 211 查看
这里要加淘宝的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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: