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);
}
}
}