httprequest,2个页面代码:
Case "details.htm" ’ 设计思路:进入这个页面,如果没有获得openid,需要先授权,然后再进入details.htm
If e.Cookies.ContainsKey("openid") Then
Functions.Execute("details",e)
Return
Else
wb.InsertHTML("<meta http-equiv='Refresh' c>") '直接跳转到网页授权页面,获取openid后,将再次进入per_xianx_yj.htm页面
e.WriteString(wb.Build) '生成网页
End If
Case "wangyeshouquan.htm" '网页简单授权页面
Dim wangzhi As String '定义,网址
For Each key As String In e.GetValues.Keys '接受获取openid成功后,转向的网页地址
If key = "yemian" Then '网址页面
wangzhi = e.GetValues(key)
msgbox("想要进入的网址=" & wangzhi) '比如之前想要进入details.htm页面,但是进入这里,wangzhi的值是上面传递的:details,不为空
End If
Next
Dim OpenID As String
Dim sb As New StringBuilder
sb.AppendLine("<meta name='viewport' c>")
If e.GetValues.ContainsKey("code") Then '如果通过授权链接跳转而来,就根据传递过来的code参数调用接口,获取用户的OpenID
Dim ul As String = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code"
ul = CExp(ul,"正常值","正常值",e.GetValues("code")) '这是填写自己的appid|appsecret
Dim hc As new HttpClient(ul)
Dim jo As JObject = JObject.Parse(hc.GetData)
If jo("openid") IsNot Nothing Then
OpenID = jo("openid")
wb.AppendCookie("openid",OpenID,31680) '22天
'e.AppendCookie("openid",OpenID) '将openid存储在Cookie中
End If
Else
OpenId = e.Cookies("openid") '否则从cookie中提取openid
End If
If e.GetValues.ContainsKey("code") = False Then '如果授权失败,且不是通过授权链接跳转而来,那么就跳转到授权链接
Dim ul1 As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state={2}#wechat_redirect"
Dim ul2 As String = UrlEncode("http://www.zm2hd.com/wangyeshouquan.htm") ’这里授权失败要加上参数是code和state?但是这里e.GetValues.ContainsKey("code") = False,没有code参数啊
ul1 = CExp(ul1,"wxaa640e55d6f95bdd",ul2,"123")
sb.Append("<meta http-equiv='Refresh' c>") '跳转到授权链接
e.WriteString(sb.ToString)
Return
End If
'msgbox("OpenId=" & OpenId)
If e.GetValues.ContainsKey("code") = False Then
sb.AppendLine("获取openid失败")
Else
msgbox("网址合成前=" & wangzhi)
’这里执行了,说明授权已经成功,前一个粉色msgbox处,提示wangzhi不为空,但是这里提示wangzhi=空,导致下面sb.Append无法合成网址
wangzhi = wangzhi & ".htm"
sb.Append("<meta http-equiv='Refresh' c>") '直接跳转到网页授权页面,获取openid后,将再次进入per_xianx_yj.htm页面
e.WriteString(sb.ToString)
Return
End If
e.WriteString(sb.ToString)
Return
因为有的代码被屏蔽,完整代码如下:
此主题相关图片如下:qq图片201811232032290909090.png
[此贴子已经被作者于2018/11/23 20:33:21编辑过]