小白使用vb.net 进行微信开发,请大神点拨一下

u010136299 2016-09-05 09:15:07
我是按照一个教程弄的
首先做了一个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
...全文
1355 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
gekiranger 2016-09-06
  • 打赏
  • 举报
回复
楼主写了好多,也很复杂,最近也在研究微信公众号开发。我做了一个很简单的类似HELLOWORLD的接收程序,不过是c#的,不妨参考下,Content变量就是用户发来的收据: HttpContext HC; public void ProcessRequest(HttpContext context) { HC = context; XmlDocument xml = ReadMessage(); string Content = GetContent(xml, "Content"); } private XmlDocument ReadMessage() { string postString = string.Empty; if (HC.Request.HttpMethod.ToUpper() == "POST") { using (Stream stream = HC.Request.InputStream) { Byte[] postBytes = new Byte[stream.Length]; stream.Read(postBytes, 0, (Int32)stream.Length); postString = Encoding.UTF8.GetString(postBytes); } } XmlDocument xml = new XmlDocument(); xml.InnerXml = postString; return xml; } private string GetContent(XmlDocument xml, string NodeName) { XmlNode XN = xml.SelectSingleNode("/xml/" + NodeName); string tmp = XN.InnerText; return tmp; }
u010136299 2016-09-06
  • 打赏
  • 举报
回复
我好像知道了,get 只是测试用的,post才是正常使用的,我原来以为他们是同时用的。所以post里面也要加那个验证,然后把context.Response.Write(echoStr) 改成自己要回复的格式。如果没有其他人有更好的答案,分数就给你
引用 1 楼 gekiranger 的回复:
楼主写了好多,也很复杂,最近也在研究微信公众号开发。我做了一个很简单的类似HELLOWORLD的接收程序,不过是c#的,不妨参考下,Content变量就是用户发来的收据: HttpContext HC; public void ProcessRequest(HttpContext context) { HC = context; XmlDocument xml = ReadMessage(); string Content = GetContent(xml, "Content"); } private XmlDocument ReadMessage() { string postString = string.Empty; if (HC.Request.HttpMethod.ToUpper() == "POST") { using (Stream stream = HC.Request.InputStream) { Byte[] postBytes = new Byte[stream.Length]; stream.Read(postBytes, 0, (Int32)stream.Length); postString = Encoding.UTF8.GetString(postBytes); } } XmlDocument xml = new XmlDocument(); xml.InnerXml = postString; return xml; } private string GetContent(XmlDocument xml, string NodeName) { XmlNode XN = xml.SelectSingleNode("/xml/" + NodeName); string tmp = XN.InnerText; return tmp; }

16,722

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧