小白使用vb.net 进行微信开发,请大神点拨一下
我是按照一个教程弄的
首先做了一个ashx页面,里面加上这些代码,验证通过了。可验证通过之后,如何获得用户发过来的文字信息呢?
我试着加了一个“ If httpMethod = "POST" 并试着将信息原封返回,结果公众号干脆不能用了
请大神点拨一下
代码如下:
==============
Dim Token As String = "3ycaoxyspks"
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim httpMethod As String = context.Request.HttpMethod.ToUpper()
If httpMethod = "GET" Then
context.Response.ContentType = "text/plain"
Dim echoStr As String = context.Request("echoStr")
If Not String.IsNullOrEmpty(echoStr) Then
Dim signature As String = context.Request.QueryString("signature") ' 微信加密签名
Dim timestamp As String = context.Request.QueryString("timestamp") ' 时间戳
Dim nonce As String = context.Request.QueryString("nonce") ' 随机数
echoStr = context.Request.QueryString("echostr") ' 随机字符串
' 微信请求参数非空验证
If Not String.IsNullOrEmpty(signature) AndAlso Not String.IsNullOrEmpty(timestamp) AndAlso Not String.IsNullOrEmpty(nonce) AndAlso Not String.IsNullOrEmpty(echoStr) Then
If CheckSignature(signature, timestamp, nonce, Token) Then
context.Response.Write(echoStr) '验证通过,响应微信公众平台后台服务器
‘这里该怎样获取客户发过来的文字信息?’
Exit Sub
End If
End If
End If
End If
End Sub
Public Function CheckSignature(ByVal signature As String, ByVal timestamp As String, ByVal nonce As String, ByVal WeiXinToken As String) As Boolean
Dim stringList As New List(Of String)() From {WeiXinToken, timestamp, nonce}
' 字典排序
stringList.Sort()
Return Sha1Encrypt(String.Join("", stringList)) = signature
End Function
''' <summary>
''' 对字符串SHA1加密
''' </summary>
''' <param name="targetString">源字符串</param>
''' <returns>加密后的十六进制字符串</returns>
Private Function Sha1Encrypt(ByVal targetString As String) As String
Dim byteArray As Byte() = Encoding.[Default].GetBytes(targetString)
Dim hashAlgorithm As System.Security.Cryptography.HashAlgorithm = New System.Security.Cryptography.SHA1CryptoServiceProvider()
byteArray = hashAlgorithm.ComputeHash(byteArray)
Dim stringBuilder As New StringBuilder()
For Each item As Byte In byteArray
stringBuilder.AppendFormat("{0:x2}", item)
Next
Return stringBuilder.ToString()
End Function