以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  实在是看不到如何使用达达的api接口,有大大能写一个简单的案例吗?  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=105812)

--  作者:chen3728060
--  发布时间:2017/8/26 15:52:00
--  实在是看不到如何使用达达的api接口,有大大能写一个简单的案例吗?

我现在想下一个简单的通过达达平台下单的功能,这个是达达开放平台的接口文档

https://newopen.imdada.cn/#/development/file/mustread?_k=hkqkna

 

我已经注册好了一个开发者号码

app_key:dadac4300b3da8c2910

app_secret:0ed05802d9537f91dc0548ae3382efca

 

然后我想模拟发送一个订单请求

https://newopen.imdada.cn/#/development/file/add?_k=qrwm7l

 

但是看了很久都不懂,狐表的帮助文档我也看了《阿里云全国物流查询接口》,但是这个是通过get的方法获取,这个达达好像要我通过post的方法,实在是不知道怎么搞。还说要

请设置Header的Content-Type为application/json,也不知道是哪里可以设置,一头雾水

 


--  作者:有点甜
--  发布时间:2017/8/27 13:16:00
--  

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

 

 


--  作者:chen3728060
--  发布时间:2017/8/27 14:29:00
--  回复:(有点甜) 比较麻烦。对方提供的是 java、...

他那些接口思路,没办法套用到vb.net上吗?是不是有几个参数是php或者java专有的?


--  作者:有点甜
--  发布时间: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编辑过]

--  作者:有点甜
--  发布时间:2017/8/27 15:00:00
--  
 看懂代码和注释,对应的修改成自己的代码使用。4楼的只是一个示例。
--  作者:chen3728060
--  发布时间:2017/8/27 15:53:00
--  回复:(有点甜) 看懂代码和注释,对应的修改成...

你这些,是不是哪里机翻的?还是人打的?总感觉,看起来,像是javaDemo机翻过来的vb.net


--  作者:有点甜
--  发布时间:2017/8/27 16:02:00
--  

百度找工具。

 

 

https://www.baidu.com/baidu?wd=java%E8%BD%ACc%23&tn=monline_dg&ie=utf-8

 

https://www.baidu.com/baidu?wd=c%23%E8%BD%ACvb.net&tn=monline_dg&ie=utf-8

 


--  作者:chen3728060
--  发布时间:2017/8/28 15:33:00
--  

我尽力,能够发送给官方,并且官方告诉我错误的地方,签名错误,然而我认为我没有错误,不知道怎么修改了,已经是尽力了,贴出所有代码,给后来的人,如果你有恒心,继续搞搞吧

 

1.全局代码里先添加MD5 32位加密的方法(狐表里默认的是24位的,不合适用)

 

Public Function MD5(ByVal strSource As String, ByVal Code As Int16) As String
Dim dataToHash As Byte() = (New System.Text.ASCIIEncoding).GetBytes(strSource)
Dim hashvalue As Byte() = CType(System.Security.Cryptography.CryptoConfig.CreateFromName("MD5"), System.Security.Cryptography.HashAlgorithm).ComputeHash(dataToHash)
Dim ATR As String = ""
Dim i As Integer
S elect Case Code
    Case 16      \'选择16位字符的加密结果
        For i = 4 To 11
            ATR &= Hex(hashvalue(i)).PadLeft(2, "0").ToLower
            
        Next
    Case 32      \'选择32位字符的加密结果
        For i = 0 To 15
            ATR &= Hex(hashvalue(i)).PadLeft(2, "0").ToLower
        Next
    Case Else       \'Code错误时,返回全部字符串,即32位字符
        For i = 0 To 15
            ATR &= Hex(hashvalue(i)).PadLeft(2, "0").ToLower
        Next
End S elect
Return ATR
End Function
 

2.在命令窗口里

 

Dim appKey As String = "dadac4300b3da8c2910"
Dim appSecret As String = "0ed05802d9537f91dc0548ae3382efca"
\' 测试环境发单url
Dim ADD_ORDER_URL As String = "http://newopen.qa.imdada.cn/api/order/addOrder"

\'根据业务需求按照文档构造请求参数
Dim jo As New JObject
jo("origin_id") = format(Date.now, "yyyyMMddHHmmss")
jo("city_name") = "上海"
jo("city_code") = "021"
jo("pay_for_supplier_fee") = 0.0
jo("fetch_from_receiver_fee") = 0.0
jo("deliver_fee") = 0.0
jo("tips") = 0
jo("info") = "测试订单"
jo("cargo_price") = 10
jo("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()) \'时间戳
jo("expected_fetch_time") = timestamp
timestamp = CInt((Date.Now.AddHours(3)- st).TotalSeconds()) \'时间戳
jo("expected_finish_time") = timestamp
jo("invoice_title") = "测试"
jo("receiver_name") = "测试"
jo("receiver_address") = "上海市崇明岛"
jo("receiver_phone") = "18588888888"
jo("receiver_tel") = "18599999999"
jo("receiver_lat") = 31.2
jo("receiver_lng") = 121.5
jo("callback") = "http://localhost:8081/receive/"
jo("shop_no") = 11047059

Dim jo1 As New JObject
jo1("body") = CompressJson(jo)
\' jo.ToString \' 注意body是json字符串
jo1("format") = "json"
Dim st2 As New Date(1970,1,1,8,0,0)
Dim timestamp2 As Integer = CInt((Date.Now - st2).TotalSeconds()) \'时间戳
jo1("timestamp") = timestamp2
jo1("app_key") = appKey
jo1("v") = "1.0"
jo1("source_id") = 73753

Dim keyList As new List (of String )
keyList.Add("body")
keyList.Add("format")
keyList.Add("timestamp")
keyList.Add("app_key")
keyList.Add("v")
keyList.Add("source_id")

Dim keyAry() As String = KeyList.ToArray()
Array.Sort(KeyAry)

Dim sign As String

For i As Integer = 0 To KeyAry.Length -1
    sign = sign & KeyAry(i)  & jo1(KeyAry(i)).ToString
Next
sign = appSecret & sign & appSecret
sign = MD5(sign,32)
sign = sign.ToUpper
jo1("signature") = sign

output.show(jo1.ToString())
Dim hc As New HttpClient(ADD_ORDER_URL)
hc.Timeout = 5 \'设置超时为5秒
hc.C
hc.Content = CompressJson(jo1)
Dim ret As String = hc.GetData()
Dim Rejo As JObject = JObject.Parse(ret)
output .Show(rejo.ToString)

 

3点执行,效果如下: 

 

{
  "body": "{\\"origin_id\\":\\"20170828153247\\",\\"city_name\\":\\"上海\\",\\"city_code\\":\\"021\\",\\"pay_for_supplier_fee\\":0.0,\\"fetch_from_receiver_fee\\":0.0,\\"deliver_fee\\":0.0,\\"tips\\":0,\\"info\\":\\"测试订单\\",\\"cargo_price\\":10,\\"is_prepay\\":0,\\"expected_fetch_time\\":1503912768,\\"expected_finish_time\\":1503916368,\\"invoice_title\\":\\"测试\\",\\"receiver_name\\":\\"测试\\",\\"receiver_address\\":\\"上海市崇明岛\\",\\"receiver_phone\\":\\"18588888888\\",\\"receiver_tel\\":\\"18599999999\\",\\"receiver_lat\\":31.2,\\"receiver_lng\\":121.5,\\"callback\\":\\"http://localhost:8081/receive/\\",\\"shop_no\\":11047059}",
  "format": "json",
  "timestamp": 1503905568,
  "app_key": "dadac4300b3da8c2910",
  "v": "1.0",
  "source_id": 73753,
  "signature": "715B3EAA8E701A05D20BC9CB653A87A1"
}

 

 


{
  "status": "fail",
  "errorCode": 2003,
  "code": 2003,
  "msg": "签名错误"
}

 

 

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

--  作者:有点甜
--  发布时间:2017/8/28 16:02:00
--  

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

 

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


--  作者:chen3728060
--  发布时间:2017/8/28 19:13:00
--  回复:(有点甜)汗,4楼例子很简单了啊。好吧,你参考...

我曹,我终于发现我的问题所在,原来是用了非正规的MD5加密!我一行行代码去对比结果,然后我发现只需要改动MD5加密的地方,就返回成功了!

sign = MD5(sign,32)
改为

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

 

 

事情起源于我发现狐表的md5加密好像跟正规的不一样,于是我去搜论坛,发现这个帖子

 

http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=60429&skin=0

 

里面介绍了MD5的32位加密的2个方法,

 

第一种MD5加密方法

把下面的代码写到全局代码,然后调用 msgbox(MD5("12345678", 32))


Public Function MD5(ByVal strSource As String, ByVal Code As Int16) As String
Dim dataToHash As Byte() = (New System.Text.ASCIIEncoding).GetBytes(strSource)
Dim hashvalue As Byte() = CType(System.Security.Cryptography.CryptoConfig.CreateFromName("MD5"), System.Security.Cryptography.HashAlgorithm).ComputeHash(dataToHash)
Dim ATR As String = ""
Dim i As Integer
Se lect Case Code
    Case 16      \'选择16位字符的加密结果
        For i = 4 To 11
            ATR &= Hex(hashvalue(i)).PadLeft(2, "0").ToLower
            
        Next
    Case 32      \'选择32位字符的加密结果
        For i = 0 To 15
            ATR &= Hex(hashvalue(i)).PadLeft(2, "0").ToLower
        Next
    Case Else       \'Code错误时,返回全部字符串,即32位字符
        For i = 0 To 15
            ATR &= Hex(hashvalue(i)).PadLeft(2, "0").ToLower
        Next
End Sele ct
Return ATR
End Function
 
第二种MD5加密方法
msgbox(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("加密内容", "MD5"))
 
我随便尝试了一下,在大多数的情况下,这2个玩意结果一样
 
sign ="123213sfsdfsd"
output.show(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sign, "MD5"))
output.show( MD5(sign,32))
 
结果:
23D78377338174648057AF6CA0A32909
23D78377338174648057AF6CA0A32909

但是在加密内容很长很恶心下,这2个玩意加密出来的就不同了!
 
加密内容:
app_keydada00fe324e250fa4fbody{"origin_id":"20170828180639","city_name":"上海","city_code":"021","pay_for_supplier_fee":0.0,"fetch_from_receiver_fee":0.0,"deliver_fee":0.0,"tips":0,"info":"测试订单","cargo_price":10,"is_prepay":0,"expected_fetch_time":1503921999,"expected_finish_time":1503925599,"invoice_title":"测试","receiver_name":"测试","receiver_address":"上海市崇明岛","receiver_phone":"18588888888","receiver_tel":"18599999999","receiver_lat":31.2,"receiver_lng":121.5,"callback":"http://localhost:8081/receive/","shop_no":11047059}formatjsonsource_id73753timestamp1503914799v1.0
 
 
结果:
4084C516E52C4C0E39818613960E1F8E
E9493EA3D025612ABB9804CFAB534A25
第二行才是对的!这2个加密方法的玩意有啥不同?不是都是MD5的32位加密吗?
[此贴子已经被作者于2017/8/28 19:18:41编辑过]