3.3 打开内部函数,增加一些函数
a、定义GET函数:HttpGet
Dim url As String = args(0)
Dim encoding As System.Text.Encoding
If args.length > 2 Then encoding = args(1)
If encoding Is Nothing Then encoding = System.Text.Encoding.UTF8
Dim wc As New System.Net.WebClient()
wc.Encoding = encoding
Return wc.DownloadString(url)
b、定义POST函数:HttpPost
'定义POST函数
Dim url As String = Args(0) 'post的接口地址
Dim postStream As System.IO.Stream = Args(1) '向接口POST的数据流
Dim encoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("utf-8")'编码格式
Dim sresult As String
Dim request As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create(url), System.Net.HttpWebRequest)
request.Method = "POST"
request.Timeout = Vars("TimeOut")
'设置标头
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = IIf(Not postStream Is Nothing, postStream.Length, 0)
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
request.KeepAlive = True
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36"
'向接口POST数据
If (Not postStream Is Nothing) Then
postStream.Position = 0
Dim requestStream As System.IO.Stream = request.GetRequestStream
Dim buffer4 As Byte() = New Byte(&H400 - 1) {}
Dim num3 As Integer = postStream.Read(buffer4, 0, buffer4.Length)
Do While (num3 <> 0)
requestStream.Write(buffer4, 0, num3)
num3 = postStream.Read(buffer4, 0, buffer4.Length)
Loop
postStream.Close()
End If
'获取接口的返回值
Dim response As System.Net.HttpWebResponse = DirectCast(request.GetResponse, System.Net.HttpWebResponse)
Using stream3 As System.IO.Stream = response.GetResponseStream
Using reader As System.IO.StreamReader = New System.IO.StreamReader(stream3, encoding)
sresult = reader.ReadToEnd
End Using
End Using
Return sresult
c、定义获取AccessToken的函数:
在全局代码中定义一个AccessToken缓存对象
Public _accesstokencache As new AccessTokenCacheData
在项目事件AfterOpenProject中赋值开发者ID
'定义开发者ID,实际应用时,可以加密字符串,放到配置文件中
_accesstokencache.AppId= "AppId" '填写自己的AppId
_accesstokencache.AppSecret = "AppSecret" '填写自己的AppSecret
创建一个函数,从微信接口获取AccessToken:GetNewAccessToken
Dim appid As String = _accesstokencache.AppId
Dim secret As String = _accesstokencache.AppSecret
Dim grant_type As String = "client_credential"
Dim url As String = String.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type={0}&appid={1}&secret={2}", grant_type, appid, secret)
Dim json As String = Functions.Execute("HttpGet",url)
Return ConvertHelper.FromJson(Of AccessTokenData)(json)
为了实现缓存,定义另外一个获取AccessToken的函数:GetAccessToken
'如果没有过期
If Not _accesstokencache.IsExpire Then
Functions.Execute("LogText","请求AccessToken:" & _accesstokencache.AccessTokenData.access_token)
Return _accesstokencache.AccessTokenData
End If
'过期就获取新的AccessToken
Dim accesstoken = Functions.Execute("GetNewAccessToken")
'第一次请求不成功,重复一次
If accesstoken.errcode <> ReturnCode.请求成功 Then
accesstoken = Functions.Execute("GetNewAccessToken")
End If
If accesstoken.errcode = ReturnCode.请求成功 Then
_accesstokencache.AccessTokenData = accesstoken
End If
Functions.Execute("LogText","请求AccessToken:" & accesstoken.access_token)
Return accesstoken
[此贴子已经被作者于2015/11/25 11:01:42编辑过]