调用微信接口取数据
2015-01-05 15:16
120 查看
添加引用:
using System.Web.Script.Serialization;
using System.Collections.Generic;
using Jayrock.Json;
public void GetInfos() {
var code = Request.QueryString["code"];
string json = string.Empty;
string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
using (WebResponse response2 = request.GetResponse())
{
using (StreamReader reader = new StreamReader(response2.GetResponseStream(), Encoding.UTF8))
{
//得到url返回的数据
json = reader.ReadToEnd();
}
} //下面可以解析了
if (!string.IsNullOrEmpty(json))
{
//需要添加引用System.Web.Extensions
//然后还要添加using System.Web.Script.Serialization;
JavaScriptSerializer jss = new JavaScriptSerializer();
Dictionary<string, string> dic = jss.Deserialize<Dictionary<string, string>>(json);
string[] js = json.Replace("{", "").Replace("}", "").Split(',');
token = js[0].ToString().Split(':')[1].ToString().Substring(1, js[0].ToString().Split(':')[1].ToString().Length-2);
openid = js[3].ToString().Split(':')[1].ToString().Substring(1, js[3].ToString().Split(':')[1].ToString().Length-2);
}
}
using System.Web.Script.Serialization;
using System.Collections.Generic;
using Jayrock.Json;
public void GetInfos() {
var code = Request.QueryString["code"];
string json = string.Empty;
string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
using (WebResponse response2 = request.GetResponse())
{
using (StreamReader reader = new StreamReader(response2.GetResponseStream(), Encoding.UTF8))
{
//得到url返回的数据
json = reader.ReadToEnd();
}
} //下面可以解析了
if (!string.IsNullOrEmpty(json))
{
//需要添加引用System.Web.Extensions
//然后还要添加using System.Web.Script.Serialization;
JavaScriptSerializer jss = new JavaScriptSerializer();
Dictionary<string, string> dic = jss.Deserialize<Dictionary<string, string>>(json);
string[] js = json.Replace("{", "").Replace("}", "").Split(',');
token = js[0].ToString().Split(':')[1].ToString().Substring(1, js[0].ToString().Split(':')[1].ToString().Length-2);
openid = js[3].ToString().Split(':')[1].ToString().Substring(1, js[3].ToString().Split(':')[1].ToString().Length-2);
}
}
相关文章推荐
- 微信小程序调用接口返回数据或提交数据
- Net 调用SAP RFC接口来读取数据
- C# 利用xmlhttp根据网址调用接口获取数据
- 微信调用接口,防止Access_token过期的方法
- C#调用sap接口及返回数据到sap
- .Net 调用SAP RFC接口来读取数据实战纪实
- 用VC++2005调用用ASP.NET(C#)做的WebSerice接口进行操作数据库
- Java调用天气接口(百度天气)解析返回的JSON数据
- 调用“中国天气网”官网数据接口显示天气预报
- 微信开发之小黄鸡接口调用(免费版)智能回复
- 数据接口-数据库数据,通过接口文件调用数据到不同的平台
- [经验总结]Perl读取Excel数据并调用MSXML接口操作XML数据
- ext实现ajax调用接口加数据到grilpanel
- sql server调用数据库存储过程接口导入数据
- 通过esb报文调用接口,返回报文数据
- C# 利用xmlhttp根据网址调用接口获取数据
- 最近工作需要写了一个JS类 页面统一调用API接口数据并计算显示
- android调用国家气象局天气预报接口json数据格式解释
- Golang实现web api接口调用及web数据抓取[get post模式] 推荐
- JSON 数据传输 与调用远程接口