获取淘宝当前系统时间
1 2 3 4 | ITopClient client = new DefaultTopClient( "http://gw.api.taobao.com/router/rest" , "appkey" , "appsecret" , "json" ); TimeGetRequest req = new TimeGetRequest(); TimeGetResponse rsp = client.Execute(req); Console.WriteLine(rsp.Body); |
获取单笔交易详情
1 2 3 4 5 6 | ITopClient client = new DefaultTopClient( "http://gw.api.taobao.com/router/rest" , "appkey" , "appsecret" , "json" ); TradeFullinfoGetRequest req = new TradeFullinfoGetRequest(); req.Fields = "tid,type,status,payment,orders" ; req.Tid = 123456789L; TradeFullinfoGetResponse rsp = client.Execute(req, sessionKey); Console.WriteLine(rsp.Body); |
监听实时消息通知
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | TmcClient client = new TmcClient( "app_key" , "app_secret" , "default" ); client.OnMessage += (s, e) => { try { Console.WriteLine(e.Message.Content); Console.WriteLine(e.Message.Topic); // 默认不抛出异常则认为消息处理成功 } catch (Exception exp) { Console.WriteLine(exp.StackTrace); e.Fail(); // 消息处理失败回滚,服务端需要重发 } }; |
批量调用API
1 2 3 4 5 6 7 | BatchTopClient client = new BatchTopClient( "http://gw.api.taobao.com/router/batch" , "appkey" , "appsecret" , "json" ); TimeGetRequest timeRequest = new TimeGetRequest(); AppipGetRequest ipRequest = new AppipGetRequest(); TopBatchRequest batch = new TopBatchRequest(); batch.AddRequest(timeRequest).AddRequest(ipRequest); TopBatchResponse rsp = client.Execute(batch); Console.WriteLine(rsp.Body); |
API服务地址
消息服务地址
环境 | 服务地址 | SSL服务地址 |
---|---|---|
正式环境 | ws://mc.api.taobao.com/ | 无 |
沙箱环境 | ws://mc.api.tbsandbox.com/ | 无 |
不解释响应字符串为对象(这时候XxxResponse包含的对象为null)
1 | DefaultTopClient.SetDisableParser( true ) |
采用精简化的JSON结构返回,去除多余JSON节点
1 | DefaultTopClient.SetUseSimplifyJson( true ) |
取消API调用日志打点
1 | DefaultTopClient.SetDisableTrace( true ) |
忽略HTTPS证书检查(建议只在测试环境打开)
1 | DefaultTopClient.SetIgnoreSSLCheck( true ) |
取消响应GZIP压缩功能(GZIP压缩功能可以显著的减少网络传输,强烈建议不要取消)
1 | DefaultTopClient.SetUseGzipEncoding( false ) |
设置HTTP连接超时和读超时时间(网络环境差的情况下可以适当增大)
1 2 3 4 | // HTTP等待请求开始返回的超时时间:默认20秒 DefaultTopClient.SetTimeout(20000L) // HTTP等待读取数据完成的超时时间:默认60秒 DefaultTopClient.SetReadWriteTimeout(60000L) |
修改日志打点存储路径
1 | DefaultTopLogger.FilePath = "c:/tmp/topsdk.log" ; |
API调用出错自动重试(一般情况下ISP类的错误是可以重试成功的)
1 2 3 4 5 6 7 8 | AutoRetryTopClient client = new AutoRetryTopClient( "http://gw.api.taobao.com/router/rest" , "appkey" , "appsecret" , "json" ); client.SetMaxRetryCount( 3 ); client.SetRetryWaitTime(100L); TimeGetRequest request = new TimeGetRequest(); TimeGetResponse response = client.Execute(request); if (!response.IsError) { Console.WriteLine(response.Body); } |
API调用就近路由(根据API发起调用所在地选择就近的TOP机房进行调用)
1 2 3 4 | ClusterTopClient client = new ClusterTopClient( "http://gw.api.taobao.com/router/rest" , "appkey" , "appsecret" , "json" ); TimeGetRequest request = new TimeGetRequest(); TimeGetResponse response = client.Execute(request); Console.WriteLine(response.Body); |