本文实例为大家分享了ASP.NET微信粉丝信息接口查看代码,供大家参考,具体内容如下

微信Token实体类:

 /// <summary>
 /// 微信Token实体类
 /// </summary>
 public class WeChatTokenEntity
 {
 public string Access_token { get; set; }

 public string Expires_in { get; set; }
 }

用户信息实体类

 /// <summary>
 /// 用户实体信息类
 /// </summary>
 public class WeChatUserEntity
 {
 public string Subscribe { get; set; }

 public string Openid { get; set; }

 public string Nickname { get; set; }

 public string Sex { get; set; }

 public string City { get; set; }

 public string Province { get; set; }

 public string Country { get; set; }

 public string HeadImgUrl { get; set; }

 public string Subscribe_time { get; set; }

 public string Language { get; set; }
 }

微信辅助操作类

 public class WeChatDemo
 {
 /*
  * 步骤:
  * 1.通过appid和secret请求微信url,得到token
  * 2.通过access_token和openid得到用户信息(头像地址等)
  * 3.通过access_token和media_id得到用户发送的微信消息
  * 
  */


 string appId = "wxxxxxxxxxxxxxx";
 string appSecret = "1234567890-==687";

 string wechatUrl = "https://api.weixin.qq.com/cgi-bin/token";


 public WeChatDemo()
 {

 }

 /// <summary>
 /// 获取token信息
 /// </summary>
 /// <returns></returns>
 public WeChatTokenEntity GetWechatToken()
 {
  //请求的url地址
  string tokenUrl = string.Format(wechatUrl, appId, appSecret);
  WeChatTokenEntity myToken;

  try
  {
  //声明并实例化一个WebClient对象
  WebClient client = new WebClient();
  //从执行url下载数据
  byte[] pageData = client.DownloadData(tokenUrl);
  //把原始数据的byte数组转为字符串
  string jsonStr = Encoding.Default.GetString(pageData);
  //初始化一个JavaScriptSerializer json解析器
  //序列化注意:需要引用System.Web.Extensions
  JavaScriptSerializer jss = new JavaScriptSerializer();
  //将字符串反序列化为Token对象
  myToken = jss.Deserialize<WeChatTokenEntity>(jsonStr);
  }
  catch (WebException ex)
  {
  throw ex;
  }
  catch (Exception ex)
  {
  throw ex;
  }

  return myToken;
 }

 /// <summary>
 /// 获取用户信息
 /// </summary>
 /// <param name="accessToken"></param>
 /// <param name="openId"></param>
 /// <returns></returns>
 public WeChatUserEntity GetUserIfo(string accessToken, string openId)
 {
  WeChatUserEntity wue = new WeChatUserEntity();

  string url = "https://api.weixin.qq.com/cgi-bin/user/info";

  url = string.Format(url, accessToken, openId);

  try
  {
  WebClient wc = new WebClient();
  byte[] pageData = wc.DownloadData(url);
  string jsonStr = Encoding.UTF8.GetString(pageData);
  JavaScriptSerializer jss = new JavaScriptSerializer();
  wue = jss.Deserialize<WeChatUserEntity>(jsonStr);

  }
  catch (WebException ex)
  {
  throw ex;
  }
  catch (Exception ex)
  {
  throw ex;
  }

  return wue;
 }

 public string GetVoice(string accessToken, string mediaId)
 {
  string voiceAddress = string.Empty;
  string voiceUrl = "http://file.api.weixin.qq.com/cgi-bin/media/get";
  voiceUrl = string.Format(voiceUrl, accessToken, mediaId);

  WebClient wc = new WebClient();
  byte[] pageData = wc.DownloadData(voiceUrl);
  string jsonStr = Encoding.UTF8.GetString(pageData);

  //TODO:获取声音
  voiceAddress = jsonStr;

  return voiceAddress;
 }

 /// <summary>
 /// 时间戳转为当前时间
 /// </summary>
 /// <param name="timeStamp"></param>
 /// <returns></returns>
 public DateTime TimeStamp2DateTime(string timeStamp)
 {
  DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  long time = long.Parse(timeStamp + "0000000");
  TimeSpan toNow = new TimeSpan(time);
  return dtStart.Add(toNow);
 }

 }

主程序:

 class Program
 {
 static void Main(string[] args)
 {
  WeChatDemo wcd = new WeChatDemo();
  WeChatTokenEntity wte = wcd.GetWechatToken();
  string token = wte.Access_token;
  string openId = "ogNVpt52xxxxxxxxxxxxxxxxxx";

  Console.WriteLine("第一步:获得access_token:\n " + token + "\n");

  Console.WriteLine("第二步:获得用户信息");
  WeChatUserEntity user = wcd.GetUserIfo(token, openId);

  Console.WriteLine("\n昵称:" + user.Nickname);
  Console.WriteLine("国家:" + user.Country);
  Console.WriteLine("省份:" + user.Province);
  Console.WriteLine("城市:" + user.City);
  Console.WriteLine("语言:" + user.Language);
  Console.WriteLine("性别:" + user.Sex);
  Console.WriteLine("OpenId:" + user.Openid);
  Console.WriteLine("是否订阅:" + user.Subscribe);
  Console.WriteLine("时间:" + wcd.TimeStamp2DateTime(user.Subscribe_time));
  Console.WriteLine("头像地址:" + user.HeadImgUrl);

  Console.WriteLine("\n第三步:获取微信声音地址");
  string mediaId = "vwvnskvsldkvmsdlvkmdslkvmsld";

  string voiceAddress = wcd.GetVoice(token, mediaId);
  Console.WriteLine("声音地址:" + voiceAddress);
  Console.Read();
 }
 }

运行结果如图:

ASP.NET微信公众号查看粉丝信息接口

本文已被整理到了《ASP.NET微信开发教程汇总》,欢迎大家学习阅读。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。