要说多少次?用这种啊,写成函数调用啊
Static CreateTime As Date '记录最近一次生成access_token的时间
Static AccessTocken As String '记录最近一次生成的access_token
Dim AppId As String= "wx8acdb7df5beb68fd" '开发者ID
Dim AppSecret As String = "a5a3b06a790d4200a151e6b13c3dc263" '开发者密匙
Dim tp As TimeSpan = Date.Now - CreateTime
Dim url As String = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}"
If tp.TotalSeconds > 3600 Then
Dim hc As New HttpClient(CExp(url,AppId,AppSecret))
Dim ret As String = hc.GetData()
If ret = "" Then '如果失败,再尝试一次
hc.GetData()
End If
CreateTime = Date.Now()
Dim jo As JObject = JObject.Parse(ret)
If jo("errcode") Is Nothing Then
AccessTocken = jo("access_token")
Else
MessageBox.show("获取access_token失败,原因:" & vbcrlf & jo("errmsg").ToString)
End If
End If
Return AccessTocken