• 微信公众账户的开发者模式(一) 部分细节access_token的获取等


    十四老久没有写博客了,中间经历了,事业,感情的几分波折。现在终于稍微缓过来一点。又是一次从头开始,走在匆忙的路上。

    好了煽情完了,直接上代码了。

    基础就不说了我用的是vs2005开发的,部署在iis6.0上,微信号为:ruyishh。目前只是简单的功能。

        /// <summary>
        /// 获取access_token
        /// </summary>
        /// <returns></returns>
        public static string GetAccessToken()
        {
            WebClient webClient = new WebClient();
            Byte[] bytes = webClient.DownloadData(string.Format("{0}&appid={1}&secret={2}", m_AcessTokenUrl, m_appid, m_secret));
            string result = Encoding.GetEncoding("utf-8").GetString(bytes);
            //JObject jObj = JObject.Parse(result);
            //JToken token = jObj["access_token"];
            //return token.ToString().Substring(1, token.ToString().Length - 2);
            string []temp = result.Split(',');
            string[] tp = temp[0].Split(':');
            return tp[1].ToString().Replace('"',' ').Trim().ToString();
        }
    View Code


    关于access_token呢 我理解就相当于一个全局session吧,每次获取一个,上一个就会过期失效,开发文档上说的有效期为30天吧。至于access_token的作用呢,可以用来创建自定义菜单。

     #region   //创建自定义菜单 注意Json格式问题
           private void CreateWxMenu()
            {
                string weixin1 = "";
                weixin1 += "{
    ";
                weixin1 += ""button":[
    ";
                weixin1 += "{
    ";
               // weixin1 += ""type":"click",
    ";
                //第一个菜单
                weixin1 += ""name":"故障维修",
    ";
                weixin1 += ""sub_button":[
    ";
                weixin1 += "{
    ";
                weixin1 += ""type":"click",
    ";
                weixin1 += ""name":"宽带故障",
    ";
                weixin1 += ""key":"V1001_REPAIRBAND"
    ";
                weixin1 += "},
    ";
                weixin1 += "{
    ";
                weixin1 += ""type":"click",
    ";
                weixin1 += ""name":"固话故障",
    ";
                weixin1 += ""key":"V1001_REPAIRPHONE"
    ";
                weixin1 += "}]
    ";
                weixin1 += "},
    ";  
                //第二个菜单
                weixin1 += "{
    ";
                //weixin1 += ""type":"click",
    ";
                weixin1 += ""name":"查询服务",
    ";
                weixin1 += ""sub_button":[
    ";
                weixin1 += "{
    ";
                weixin1 += ""type":"click",
    ";
                weixin1 += ""name":"话费余额",
    ";
                weixin1 += ""key":"V1002_TELQUERY"
    ";
                weixin1 += "},
    ";
    
                weixin1 += "{
    ";
                weixin1 += ""type":"click",
    ";
                weixin1 += ""name":"流量查询",
    ";
                weixin1 += ""key":"V1002_FLOWQUERY"
    ";
                weixin1 += "},
    ";
    
                weixin1 += "{
    ";
                weixin1 += ""type":"click",
    ";
                weixin1 += ""name":"积分查询",
    ";
                weixin1 += ""key":"V1002_TOTALQUERY"
    ";
                weixin1 += "},
    ";
    
                weixin1 += "{
    ";
                weixin1 += ""type":"click",
    ";
                weixin1 += ""name":"账单查询",
    ";
                weixin1 += ""key":"V1002_LISTQUERY"
    ";
                weixin1 += "}]
    ";
                weixin1 += "},
    ";  
                //第三个菜单
                weixin1 += "{
    ";
                weixin1 += ""name":"套餐服务",
    ";
                weixin1 += ""sub_button":[
    ";
                weixin1 += "{
    ";
                weixin1 += ""type":"click",
    ";
                weixin1 += ""name":"最新业务",
    ";
                weixin1 += ""key":"V1003_NEWS"
    ";
                weixin1 += "},
    ";
    
                weixin1 += "{
    ";
                weixin1 += ""type":"click",
    ";
                weixin1 += ""name":"流量套餐",
    ";
                weixin1 += ""key":"V1003_SETMEAL"
    ";
                weixin1 += "},
    ";
    
                weixin1 += "{
    ";
                weixin1 += ""type":"click",
    ";
                weixin1 += ""name":"短信套餐",
    ";
                weixin1 += ""key":"V1003_MESSAGE"
    ";
                weixin1 += "}]
    ";
                weixin1 += "}
    ";
                weixin1 += "}]
    ";
    
                weixin1 += "}
    ";
               
                PostMenuData( postUrl+GetAccessToken(),weixin1 );
            }
        
    
        private void PostMenuData(string url, string postData) 
        {
            Stream outstream = null;
            Stream instream = null;
            StreamReader sr = null;
            HttpWebResponse response = null;
            HttpWebRequest request = null;
            Encoding encoding = Encoding.UTF8;
            byte[] data = encoding.GetBytes(postData);
            // 准备请求...
            try
            {
                // 设置参数
                request = WebRequest.Create(url) as HttpWebRequest;
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.AllowAutoRedirect = true;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                outstream = request.GetRequestStream();
                outstream.Write(data, 0, data.Length);
                outstream.Close();
                //发送请求并获取相应回应数据
                response = request.GetResponse() as HttpWebResponse;
                //直到request.GetResponse()程序才开始向目标网页发送Post请求
                instream = response.GetResponseStream();
                sr = new StreamReader(instream, encoding);
                //返回结果网页(html)代码
                string content = sr.ReadToEnd();
                string err = string.Empty;
               // return content;
            }
            catch (Exception ex)
            {
                string err = ex.Message;
                //return string.Empty;
            }
        }
    View Code


    关系图我就不画了,运行逻辑呢就是这样:  微信用户 <>     腾讯微信服务器   <> 我的程序

    对于处理程序代码如下:

       protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Request.HttpMethod.ToLower() == "post")
            {
                Stream s = System.Web.HttpContext.Current.Request.InputStream;
                byte[] b = new byte[s.Length];
                s.Read(b, 0, (int)s.Length);
                postStr = Encoding.UTF8.GetString(b);
    
                if (!string.IsNullOrEmpty(postStr))
                {
                    ResponseMsg(); //对微信服务器psot形式发来的消息进行处理
                }
            } else {
                string echoStr = System.Web.HttpContext.Current.Request.QueryString["echoStr"];
                if (CheckSignature())
                {
                    if( !string.IsNullOrEmpty(  echoStr ) )
                    System.Web.HttpContext.Current.Response.Write(echoStr); //返回原值表示校验成功
                    System.Web.HttpContext.Current.Response.End();
                }                 
            }
           
        }
    
        //返回微信信息
        private void ResponseMsg()
        {
            在此写你的处理代码
        }
    View Code

  • 相关阅读:
    8.2.8 A Simple Game
    8.2.7 Be the Winner
    8.2.6 John
    8.2.5 kiki's game
    8.2.2 Good Luck in CET-4 Everybody!
    8.2.4 悼念512汶川大地震遇难同胞——选拔志愿者
    8.1.8 Team Queue
    8.2.1 Brave Game
    8.1.7 愚人节的礼物
    8.1.6 Windows Message Queue
  • 原文地址:https://www.cnblogs.com/yanghucheng/p/3382513.html
Copyright © 2020-2023  润新知