获取微信的access_tokey,处理json格式的数据

  #region 获取微信凭证
        public string GetAccessToken(string wechat_id)
        {
            string accessToken = "";

            DataTable dtwecaht = wechatdal.GetList("wechat_id='" + wechat_id + "'").Tables[0];

            if (dtwecaht.Rows.Count > 0)
            {
                string respText = "";
                string wechat_appid = dtwecaht.Rows[0]["wechat_appid"].ToString();
                string wechat_appsecret = dtwecaht.Rows[0]["wechat_appsecret"].ToString();
                string url = string.Format(getAccessTokenUrl, wechat_appid, wechat_appsecret);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                using (Stream resStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(resStream, Encoding.Default);
                    respText = reader.ReadToEnd();
                    resStream.Close();
                }
                JavaScriptSerializer Jss = new JavaScriptSerializer();
                Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(respText);

                accessToken = respDic["access_token"].ToString();
            }

            return accessToken;
        }
        #endregion 获取微信凭证

  通过JavaScriptSerializer将json转换为对象,获取值

posted @ 2014-09-28 16:36  双魂人生  阅读(4345)  评论(0编辑  收藏  举报