淘宝网开放平台API(taobao.taobaoke.items.get)调用示例
编辑:dnawo 日期:2009-07-01
复制内容到剪贴板 程序代码
<%@ WebHandler Language="C#" Class="Taobaoke" %>
using System;
using System.Web;
using System.Net;
using System.Xml;
using System.Text;
using System.Web.Security;
using System.Collections.Generic;
public class Taobaoke : IHttpHandler {
public void ProcessRequest (HttpContext context) {
//1.应用信息
string app_key = "10011201";
string app_secret = "0fd3ffcb7008570b95670ec5ad3fe201";
//2.参数集
SortedList<string, string> parameters = new SortedList<string, string>();
//2.1 应用级输入参数
parameters.Add("fields", "iid,title,nick,pic_url,price,click_url");
parameters.Add("pid", "mm_12851146_0_0");
parameters.Add("keyword", "竹炭");
parameters.Add("cid", "");
parameters.Add("strat_price", "");
parameters.Add("end_price", "");
parameters.Add("auto_send", "");
parameters.Add("area", "福州");
parameters.Add("credit", "");
parameters.Add("sort", "price_asc");
parameters.Add("is_guarantee", "");
parameters.Add("page_no", "1");
parameters.Add("page_size", "40");
//2.2 系统级参数
parameters.Add("method", "taobao.taobaoke.items.get");
parameters.Add("session", "");
parameters.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
parameters.Add("format", "xml");
parameters.Add("app_key", app_key);
parameters.Add("v", "1.0");
//2.3 生成sign,格式: app_secret参数1值1参数2值2,生成md5后转为大写
StringBuilder sb = new StringBuilder();
sb.Append(app_secret);
foreach (KeyValuePair<string, string> item in parameters)
{
sb.Append(item.Key + item.Value);
}
parameters.Add("sign", FormsAuthentication.HashPasswordForStoringInConfigFile(sb.ToString(), "MD5").ToUpper());
//3.生成url
string url = "http://gw.sandbox.taobao.com/router/rest?";//线上环境: http://gw.api.taobao.com/router/rest 测试环境: http://gw.sandbox.taobao.com/router/rest
StringBuilder query = new StringBuilder();
foreach (KeyValuePair<string, string> item in parameters)
{
query.Append(item.Key + "=" + System.Web.HttpUtility.UrlEncode(item.Value, System.Text.Encoding.UTF8) + "&");
}
url += query.ToString().TrimEnd(new char[] { '&' });
//4.淘宝客商品查询
try
{
WebRequest webRequest = WebRequest.Create(new Uri(url));
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
webRequest.Timeout = 5000;
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(((WebResponse)webRequest.GetResponse()).GetResponseStream());
context.Response.ContentType = "text/xml";
context.Response.Write(xmlDocument.InnerXml);
}
catch (Exception ex)
{
context.Response.Write(ex.Message);
}
}
public bool IsReusable {
get {
return false;
}
}
}
using System;
using System.Web;
using System.Net;
using System.Xml;
using System.Text;
using System.Web.Security;
using System.Collections.Generic;
public class Taobaoke : IHttpHandler {
public void ProcessRequest (HttpContext context) {
//1.应用信息
string app_key = "10011201";
string app_secret = "0fd3ffcb7008570b95670ec5ad3fe201";
//2.参数集
SortedList<string, string> parameters = new SortedList<string, string>();
//2.1 应用级输入参数
parameters.Add("fields", "iid,title,nick,pic_url,price,click_url");
parameters.Add("pid", "mm_12851146_0_0");
parameters.Add("keyword", "竹炭");
parameters.Add("cid", "");
parameters.Add("strat_price", "");
parameters.Add("end_price", "");
parameters.Add("auto_send", "");
parameters.Add("area", "福州");
parameters.Add("credit", "");
parameters.Add("sort", "price_asc");
parameters.Add("is_guarantee", "");
parameters.Add("page_no", "1");
parameters.Add("page_size", "40");
//2.2 系统级参数
parameters.Add("method", "taobao.taobaoke.items.get");
parameters.Add("session", "");
parameters.Add("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
parameters.Add("format", "xml");
parameters.Add("app_key", app_key);
parameters.Add("v", "1.0");
//2.3 生成sign,格式: app_secret参数1值1参数2值2,生成md5后转为大写
StringBuilder sb = new StringBuilder();
sb.Append(app_secret);
foreach (KeyValuePair<string, string> item in parameters)
{
sb.Append(item.Key + item.Value);
}
parameters.Add("sign", FormsAuthentication.HashPasswordForStoringInConfigFile(sb.ToString(), "MD5").ToUpper());
//3.生成url
string url = "http://gw.sandbox.taobao.com/router/rest?";//线上环境: http://gw.api.taobao.com/router/rest 测试环境: http://gw.sandbox.taobao.com/router/rest
StringBuilder query = new StringBuilder();
foreach (KeyValuePair<string, string> item in parameters)
{
query.Append(item.Key + "=" + System.Web.HttpUtility.UrlEncode(item.Value, System.Text.Encoding.UTF8) + "&");
}
url += query.ToString().TrimEnd(new char[] { '&' });
//4.淘宝客商品查询
try
{
WebRequest webRequest = WebRequest.Create(new Uri(url));
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
webRequest.Timeout = 5000;
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(((WebResponse)webRequest.GetResponse()).GetResponseStream());
context.Response.ContentType = "text/xml";
context.Response.Write(xmlDocument.InnerXml);
}
catch (Exception ex)
{
context.Response.Write(ex.Message);
}
}
public bool IsReusable {
get {
return false;
}
}
}
相关链接
·淘宝开放平台(TOP):http://open.taobao.com/
·API文档:http://wiki.open.taobao.com/index.php/API%E6%96%87%E6%A1%A3
·API测试工具:http://open.taobao.com/api_tool/api_test.html
上一篇: 详解window.setInterval方法
下一篇: 淘宝网开放平台(TOP)被授权访问API的9类角色级别
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
下一篇: 淘宝网开放平台(TOP)被授权访问API的9类角色级别
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
评论: 2 | 引用: 0 | 查看次数: 12536
沙发
这个帖子不错,我就是参照了这段代码实现调用的。
福到口
www.foodcoo.com/stuff/c-50003695-p-1.html
不过最近又碰到另外2个问题:
1. taobao.taobaoke.listurl.get 这个方法好像老是失败。
2. 如何在检索页面调用taobaoke?因为用户搜索的关键词通常和产品不太相干。可能找不到结果。能做个语义匹配吗?
福到口
www.foodcoo.com/stuff/c-50003695-p-1.html
不过最近又碰到另外2个问题:
1. taobao.taobaoke.listurl.get 这个方法好像老是失败。
2. 如何在检索页面调用taobaoke?因为用户搜索的关键词通常和产品不太相干。可能找不到结果。能做个语义匹配吗?
发表评论
请登录后再发表评论!
2.taobao.taobaoke.items.get,这个API可以搜索淘宝客产品。不太明白语义匹配的意思