星空网 > 软件开发 > ASP.net

微信开发基础教程

微信开发学习笔记----

1.正确填写服务器配置信息

微信开发基础教程

其中Url为我们的要接收并处理微信服务器发送的消息的一般处理程序地址,如:http://sadi.qrenlei.cn/WXTest.ashx

Token是一个开发者自定义的验证字符串,可任意填写。

点击提交前,需要把咱们的包含一般处理程序的网站发布到服务器上。

2.一般处理程序的编写

微信开发基础教程

 

 

     if (Request.HttpMethod.ToLower() == "get")      {        Validate();      }     public void Validate()    {         //微信接口接入验证代码        string signature = Request["signature"];        string token = "Your token";        string timestamp = Request["timestamp"];        string nonce = Request["nonce"];        string echostr = Request["echostr"];        string[] temp = { token, timestamp, nonce };        Array.Sort(temp);        string str = string.Join("", temp);        string sha1Str = FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1");        if (sha1Str.ToLower() == signature.ToLower())        {          Response.Write(echostr);        }    }

服务器端配置好后,点击“提交”按钮,就会提示成功接入的信息。

3.接收消息

当普通微信用户向公众账号发消息时,微信服务器将POST消息的

微信开发基础教程

      else if (Request.HttpMethod.ToLower() == "post")      {        //微信服务器发送信息是通过post请求,向发送者以流的形式发送        Stream  Request.InputStream;        = new = doc.DocumentElement;        string toUserName = root.SelectSingleNode("ToUserName").InnerText;        string fromUserName = root.SelectSingleNode("FromUserName").InnerText;        int createTime = int.Parse(root.SelectSingleNode("CreateTime").InnerText);        string msgType = root.SelectSingleNode("MsgType").InnerText;        string content = root.SelectSingleNode("Content").InnerText;        long msgId = Int64.Parse(root.SelectSingleNode("MsgId").InnerText);              }
     //因为服务器返回的是时间戳,即现在的时间与1970年1月1日8时0分0秒的秒数差,所以可以用此函数对时间进行处理 public DateTime GetDateTime(int timeSpan) { return new DateTime(1970,1,1,8,0,0).AddSeconds(timeSpan); }

4.返回消息

//微信服务器接收信息是通过post请求,向接收者以流的形式发送        /*         格式为:          <*/        string restring.Format(@"<", fromUserName, toUserName, GetSecond(), "已接收到你的消息[服务器自动回复]");        Response.Write(re
       public int GetSecond()      {        return (int)(DateTime.Now - new DateTime(1970, 1, 1, 8, 0, 0)).TotalSeconds;      } 

现在可以测试一下你的公众号,向公众号发送一个文本消息,公众号在5秒后会自动回复你!

 




原标题:微信开发基础教程

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流