以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- 网络异常时如何处理报错 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=175890) |
-- 作者:bobolan521 -- 发布时间: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) |
-- 作者:有点蓝 -- 发布时间: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 ……
|