以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- VB调用阿里云API接口代码分享 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=183176) |
-- 作者:otterlike -- 发布时间:2022/10/27 10:42:00 -- VB调用阿里云API接口代码分享 大家好!经过近两周时间的折腾,我总算打通了VB调用阿里云API接口,中间得到“有点蓝”老师以及其它伙伴的帮助,在此表示感谢!现在将代码分享出来,供大家参考 阿里云API参数规则如下: 完整调用代码如下: 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) |
-- 作者:有点蓝 -- 发布时间:2022/10/27 10:52:00 -- 写明一下是什么功能的接口。阿里云有很多接口,用法都不一样的。比如短信的:http://www.foxtable.com/webhelp/topics/3389.htm |