Foxtable(狐表)用户栏目专家坐堂 → 网络异常时如何处理报错


  共有3487人关注过本帖树形打印复制链接

主题:网络异常时如何处理报错

帅哥哟,离线,有人找我吗?
bobolan521
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:三尾狐 帖子:637 积分:7362 威望:0 精华:1 注册:2013/11/26 7:49:00
网络异常时如何处理报错  发帖心情 Post By:2022/3/23 10:16:00 [只看该作者]

Static CreateTime As Date '记录最近一次生成access_token的时间
Static
AccessTocken As String '记录最近一次生成的access_token
Dim
CorpID As String"ww7deed3af842576ed" '企业号的CorpID
Dim
Secret As String = "sO7RjwoisX82kB_gcRY4isEU8ZC6sJVcGs6CDlz8Nkc" '管理组的Secret
Dim
tp As TimeSpan = Date.Now - CreateTime
If
tp.TotalSeconds > 3600 Then
    Dim url As String = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}"
    Dim hc As New HttpClient(CExp(url, CorpID, Secret))
    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("access_token") IsNot Nothing Then
       
AccessTocken = jo("access_token")
    Else
        MessageBox.show("获取access_token失败,原因:" & vbcrlf & jo.ToString)
    End If

End
If
Return
AccessTocken

老师,这是帮助里的代码,如果公司出现断网,这个就会报出以下错误,请问一下,代码要如何修改一下,确保就算是网络异常程序也不报错!

2022-03-23 10:09:49.6230The remote name could not be resolved: 'qyapi.weixin.qq.com'
   at System.Net.HttpWebRequest.GetResponse()
   at Foxtable.HttpClient.GetData(Boolean SkipError)

2022-03-23 10:09:49.6690The remote name could not be resolved: 'qyapi.weixin.qq.com'
   at System.Net.HttpWebRequest.GetResponse()
   at Foxtable.HttpClient.GetData(Boolean SkipError)


2022-03-23 10:09:49.7170Exception has been thrown by the target of an invocation.
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Foxtable.UserFunctions.Execute(String Name, Object[] Args)
Error reading JObject from JsonReader. Path '', line 0, position 0.
   at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)
   at Newtonsoft.Json.Linq.JObject.Parse(String json, JsonLoadSettings settings)
   at UserCode.AKrWJPkM1HPiC1C9t(Object[] Args)





 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110574 积分:562760 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2022/3/23 10:20:00 [只看该作者]

……
If tp.TotalSeconds > 3600 Then
    Dim url As String = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}"
    Dim hc As New HttpClient(CExp(url, CorpID, Secret))
    Dim ret As String = hc.GetData(true)
    If ret = "" orelse not(ret like "{*") Then '
如果失败,再尝试一次
        hc.GetData(true)
    End If
If ret = "" orelse not(ret like "{*") then
msgbox("c错误" & ret)
else
    CreateTime = Date.Now()
    Dim jo As JObject = JObject.Parse(ret)
    If jo("access_token"IsNot Nothing Then
        
AccessTocken = jo("access_token")
    Else
        MessageBox.show("获取access_token失败,原因:" & vbcrlf & jo.ToString)
    End If
endif
End
 If
……

 回到顶部