Foxtable(狐表)用户栏目专家坐堂 → 实在是看不到如何使用达达的api接口,有大大能写一个简单的案例吗?


  共有2788人关注过本帖树形打印复制链接

主题:实在是看不到如何使用达达的api接口,有大大能写一个简单的案例吗?

帅哥哟,离线,有人找我吗?
有点甜
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2017/8/27 13:16:00 [显示全部帖子]

 比较麻烦。对方提供的是 java、php的demo代码,要转化成c#,然后c#转换成vb.net,vb.net再修改修改才能写到foxtable。

 

 


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2017/8/27 14:59:00 [显示全部帖子]

全局代码

 

'请使用开发者对应的 app key & app secret
Public appKey As String = "dada00fe324e250fa4f"
Public appSecret As String = "b12e055b501c26a2db1ad45603e0ed92"
' 测试环境发单url
Public ADD_ORDER_URL As String = "http://newopen.qa.imdada.cn/api/order/addOrder"

Public Function getSign(requestMap As IDictionary(Of String, Object)) As String
 '请求参数键值升序排序
 Dim keySet As Dictionary(Of String, Object).KeyCollection = requestMap.Keys
 Dim List As List(Of String) = New List(Of String)(keySet)
 List.Sort()

 '拼参数字符串.
 Dim signStr As New StringBuilder()
 For i As Integer = 0 To List.Count - 1
  Dim key As String = List(i)
  signStr.Append(key & requestMap(key))
 Next

 'MD5签名并校验
 Dim sign As String = appSecret & signStr.ToString() & appSecret

 sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sign, "MD5")
 Return sign.ToUpper()
End Function

'根据业务需求按照文档构造请求参数
Public Function  getRequestParam() As Dictionary(Of String, Object)
 Dim paramMap As Dictionary(Of String, Object) = New Dictionary(Of String, Object)()
        Dim jo As JObject = JObject.FromObject(getBodyParam())
 paramMap("body") = jo.tostring  ' 注意body是json字符串
 paramMap("format") = "json"
 Dim st As New Date(1970,1,1,8,0,0)
        Dim timestamp As Integer = CInt((Date.Now - st).TotalSeconds()) '时间戳
 paramMap("timestamp") = timestamp
 paramMap("app_key") = appKey
 paramMap("v") = "1.0"
 paramMap("source_id") = 73753
 Return paramMap
End Function

'根据业务需求按照文档构造请求参数
Public Function getBodyParam() As Dictionary(of String, object)
 Dim paramMap As new Dictionary(Of String, object)
 paramMap("origin_id") = format(Date.now, "yyyyMMddHHmmss")
 paramMap("city_name") = "上海"
 paramMap("city_code") = "021"
 paramMap("pay_for_supplier_fee") = 0.0
 paramMap("fetch_from_receiver_fee") = 0.0
 paramMap("deliver_fee") = 0.0
 paramMap("tips") = 0
 paramMap("info") = "测试订单"
 paramMap("cargo_price") = 10
 paramMap("is_prepay") = 0
 Dim st As New Date(1970,1,1,8,0,0)
        Dim timestamp As Integer = CInt((Date.Now.AddHours(2) - st).TotalSeconds()) '时间戳
 paramMap("expected_fetch_time") = timestamp
        timestamp = CInt((Date.Now.AddHours(3)- st).TotalSeconds()) '时间戳
 paramMap("expected_finish_time") = timestamp
 paramMap("invoice_title") = "测试"
 paramMap("receiver_name") = "测试"
 paramMap("receiver_address") = "上海市崇明岛"
 paramMap("receiver_phone") = "18588888888"
 paramMap("receiver_tel") = "18599999999"
 paramMap("receiver_lat") = 31.2
 paramMap("receiver_lng") = 121.5
 paramMap("callback") = "http,//localhost:8081/receive/"
 paramMap("shop_no") = 11047059

 Return paramMap

End Function


' 发送http请求,注意post请求方式,json数据格式,可以使用其他http工具
Public Function sendPost(urlStr As String, body As String) As String
 Dim req = System.Net.WebRequest.Create(urlStr)
 req.Method = "POST"
req.ContentType = "application/json;charset=UTF-8"
 Dim aryBuf As Byte() = Encoding.UTF8.GetBytes(body)
 req.ContentLength = aryBuf.Length
 Dim writer = req.GetRequestStream()
 writer.Write(aryBuf, 0, aryBuf.Length)
 writer.Close()
 writer.Dispose()
 Dim pos = req.GetResponse
 Dim stm As System.IO.Stream = pos.GetResponseStream()
 Dim reader As New System.IO.StreamReader(stm)
 Dim str As String = reader.ReadToEnd
 pos.Close
 stm.Close
 reader.close

 Return str
End Function

 

 

调用代码

 

'根据需求按照文档构造请求参数
Dim paramMap As IDictionary(Of String, Object) = getRequestParam
'按照文档生成签名
Dim sign As String = getSign(paramMap)
paramMap("signature") = sign
'发送请求

Dim jo As JObject = JObject.FromObject(paramMap)
msgbox(jo.ToString())
Dim response As String = sendPost(ADD_ORDER_URL, jo.ToString())
msgbox(response)

[此贴子已经被作者于2017/8/28 15:50:07编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2017/8/27 15:00:00 [显示全部帖子]

 看懂代码和注释,对应的修改成自己的代码使用。4楼的只是一个示例。

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2017/8/27 16:02:00 [显示全部帖子]


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2017/8/28 16:02:00 [显示全部帖子]

汗,4楼例子很简单了啊。好吧,你参考下面的例子。

 

 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:新达达物流.table


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2017/8/29 9:11:00 [显示全部帖子]

 没必要纠结这个,那就是那个md5函数写得有问题,没有考虑到特殊字符的特殊情况。

 

 直接用.net自带的简单方便快捷

 

sign =System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sign, "MD5"))


 回到顶部