Dim url As String = "http://10.72.**********es" 'api应用程序编程接口地址
Dim apiName As String = "TE**********e" 'api应用程序编程接口名称
Dim apiVersion As String = "1.0.0" '版本
Dim ak As String = "Z***********=" 'accessKey访问密钥
Dim sk As String = "b**************dbf" ' secret Key 密钥
Dim timestamp As String = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) & ""
Dim postBody As String = "{""pageNo"":1,""pageSize"":2000}"
Dim dict As Dictionary(Of String, String) = New Dictionary(Of String, String)()
dict.Add("_api_access_key", AK)
dict.Add("_api_name", apiName)
dict.Add("_api_version", apiVersion)
dict.Add("_api_timestamp", timestamp)
dict.Add("cb", postBody)
Dim lst As List(Of KeyValuePair(Of String, String)) = New List(Of KeyValuePair(Of String, String))(dict)
lst.Sort(Function(ByVal source As KeyValuePair(Of String, String), ByVal target As KeyValuePair(Of String, String)) source.Key.CompareTo(target.Key))
Dim signBuilder As StringBuilder = New StringBuilder()
For Each item As KeyValuePair(Of String, String) In lst
Dim key As String = item.Key
Dim value As String = item.Value
signBuilder.Append("&").Append(key).Append("=").Append(value)
Next
Dim signStr As String = signBuilder.ToString()
Dim m As System.Security.Cryptography.HMAC = System.Security.Cryptography.HMACSHA1.Create()
m.Key = Encoding.UTF8.GetBytes(SK)
Dim signData As Byte() = Encoding.UTF8.GetBytes(signStr)
Dim finalData As Byte() = m.ComputeHash(signData)
Dim signature As String = Convert.ToBase64String(finalData)
'创建一个web请求对象'
Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
request.Method = "POST" '设置调用方式, 必须为 post
request.ContentType = "application/json" '业务数据封装成json格式包含在POST请求的请求体中
request.Headers.Add("_api_access_key", AK)
request.Headers.Add("_api_name", apiName)
request.Headers.Add("_api_version", apiVersion)
request.Headers.Add("_api_timestamp", timestamp)
request.Headers.Add("_api_signature", signature)
'API请求参数序列化和反序列化
'定义字节数组'
Dim postdata() As System.Byte = Encoding.UTF8.GetBytes(postBody)
'设置request对象的请求字节的长度'
request.ContentLength = postdata.Length
'获取request对象的数据流'
Dim requesstream As System.IO.Stream = request.GetRequestStream()
'将数据内容填充到流中'
requesstream.Write(postdata, 0, postdata.Length)
'关闭流'
requesstream.Close()
'根据请求的request对象获取响应的response对象'
Dim response As System.Net.WebResponse = request.GetResponse()
'获取response数据流对象'
Dim responsestream As New System.IO.StreamReader(response.GetResponseStream())
Dim html As String = responsestream.ReadToEnd()
requesstream.Close()
response.Close()
'返回本次请求的响应数据'
Output.Show(html)