微信公众账户的开发者模式(一) 部分细节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 += "{\n";
            weixin1 += "\"button\":[\n";
            weixin1 += "{\n";
           // weixin1 += "\"type\":\"click\",\n";
            //第一个菜单
            weixin1 += "\"name\":\"故障维修\",\n";
            weixin1 += "\"sub_button\":[\n";
            weixin1 += "{\n";
            weixin1 += "\"type\":\"click\",\n";
            weixin1 += "\"name\":\"宽带故障\",\n";
            weixin1 += "\"key\":\"V1001_REPAIRBAND\"\n";
            weixin1 += "},\n";
            weixin1 += "{\n";
            weixin1 += "\"type\":\"click\",\n";
            weixin1 += "\"name\":\"固话故障\",\n";
            weixin1 += "\"key\":\"V1001_REPAIRPHONE\"\n";
            weixin1 += "}]\n";
            weixin1 += "},\n";  
            //第二个菜单
            weixin1 += "{\n";
            //weixin1 += "\"type\":\"click\",\n";
            weixin1 += "\"name\":\"查询服务\",\n";
            weixin1 += "\"sub_button\":[\n";
            weixin1 += "{\n";
            weixin1 += "\"type\":\"click\",\n";
            weixin1 += "\"name\":\"话费余额\",\n";
            weixin1 += "\"key\":\"V1002_TELQUERY\"\n";
            weixin1 += "},\n";

            weixin1 += "{\n";
            weixin1 += "\"type\":\"click\",\n";
            weixin1 += "\"name\":\"流量查询\",\n";
            weixin1 += "\"key\":\"V1002_FLOWQUERY\"\n";
            weixin1 += "},\n";

            weixin1 += "{\n";
            weixin1 += "\"type\":\"click\",\n";
            weixin1 += "\"name\":\"积分查询\",\n";
            weixin1 += "\"key\":\"V1002_TOTALQUERY\"\n";
            weixin1 += "},\n";

            weixin1 += "{\n";
            weixin1 += "\"type\":\"click\",\n";
            weixin1 += "\"name\":\"账单查询\",\n";
            weixin1 += "\"key\":\"V1002_LISTQUERY\"\n";
            weixin1 += "}]\n";
            weixin1 += "},\n";  
            //第三个菜单
            weixin1 += "{\n";
            weixin1 += "\"name\":\"套餐服务\",\n";
            weixin1 += "\"sub_button\":[\n";
            weixin1 += "{\n";
            weixin1 += "\"type\":\"click\",\n";
            weixin1 += "\"name\":\"最新业务\",\n";
            weixin1 += "\"key\":\"V1003_NEWS\"\n";
            weixin1 += "},\n";

            weixin1 += "{\n";
            weixin1 += "\"type\":\"click\",\n";
            weixin1 += "\"name\":\"流量套餐\",\n";
            weixin1 += "\"key\":\"V1003_SETMEAL\"\n";
            weixin1 += "},\n";

            weixin1 += "{\n";
            weixin1 += "\"type\":\"click\",\n";
            weixin1 += "\"name\":\"短信套餐\",\n";
            weixin1 += "\"key\":\"V1003_MESSAGE\"\n";
            weixin1 += "}]\n";
            weixin1 += "}\n";
            weixin1 += "}]\n";

            weixin1 += "}\n";
           
            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

 

 

posted @ 2013-10-22 15:24  十四  阅读(5675)  评论(5编辑  收藏  举报