微信服务号 接口调用

        public ActionResult Index()
        {
           //ViewBag.openid = "oCaDVv1j2bt_RjbiS1aHtfV9sn6E";
            string appid = DAL.CommWeb.Appid;
            string secret = DAL.CommWeb.Secret;
            ViewBag.appid = appid;
            ViewBag.secret = secret;
            #region 获取token
            string url_access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
            string obj_token = DAL.CommWeb.RequestUrl(url_access_token);
            dynamic dy_token = DAL.JsonParser.FromJson(obj_token);
            string access_token = dy_token.access_token;
            #endregion 
}


 

 

 

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace Mobile_Healthy.DAL
{
    public class CommWeb
    {
        public CommWeb()
        { }

        static public string Appid
        {
            get
            {
                string _appid = ConfigurationManager.AppSettings["appid"];
                return _appid;
            }
        }
        static public string Secret
        {
            get
            {
                string _secret = ConfigurationManager.AppSettings["secret"];
                return _secret;
            }
        }
        static public string RequestUrl(string url)
        {
            //{\"access_token\":\"zeOij5UaHVlkFKOj89SGgpTL2tgUFH_gFUA_oYohsPSkhWiWcjU1WaL_S-N-68O8TB9ryIf5PVeOWuggyW7g51vzH1beukT7Mv1W1KLxL-b3SilkR9cZuZ2leCvAYjdNJTKjADAPZQ\",\"expires_in\":7200}
            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);    //创建一个请求示例
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();  //获取响应,即发送请求
                Stream responseStream = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
                string html = streamReader.ReadToEnd();
                if (response != null)
                    response.Close();
                return html;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace Mobile_Healthy.DAL
{
    public class JsonParser
    {
        /// <summary>  
        /// 从json字符串到对象。  
        /// </summary>  
        /// <param name="jsonStr"></param>  
        /// <returns></returns>  
        static public dynamic FromJson(string jsonStr)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() });
            dynamic glossaryEntry = jss.Deserialize(jsonStr, typeof(object)) as dynamic;
            return glossaryEntry;
        }
    }

    public class DynamicJsonConverter : JavaScriptConverter
    {
        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            if (dictionary == null)
                throw new ArgumentNullException("dictionary");

            if (type == typeof(object))
            {
                return new DynamicJsonObject(dictionary);
            }

            return null;
        }

        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override IEnumerable<Type> SupportedTypes
        {
            get { return new ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(object) })); }
        }
    }

    public class DynamicJsonObject : DynamicObject
    {
        private IDictionary<string, object> Dictionary { get; set; }

        public DynamicJsonObject(IDictionary<string, object> dictionary)
        {
            this.Dictionary = dictionary;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = this.Dictionary[binder.Name];

            if (result is IDictionary<string, object>)
            {
                result = new DynamicJsonObject(result as IDictionary<string, object>);
            }
            else if (result is ArrayList && (result as ArrayList) is IDictionary<string, object>)
            {
                result = new List<DynamicJsonObject>((result as ArrayList).ToArray().Select(x => new DynamicJsonObject(x as IDictionary<string, object>)));
            }
            else if (result is ArrayList)
            {
                result = new List<object>((result as ArrayList).ToArray());
            }

            return this.Dictionary.ContainsKey(binder.Name);
        }
    }
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bingo_BIG

你的鼓励是我最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值