全局代码
'请使用开发者对应的 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编辑过]