如何使用.NET查看微信公众号关注者接口
发表于:2025-01-17 作者:千家信息网编辑
千家信息网最后更新 2025年01月17日,如何使用.NET查看微信公众号关注者接口,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。实体类: public class userl
千家信息网最后更新 2025年01月17日如何使用.NET查看微信公众号关注者接口
如何使用.NET查看微信公众号关注者接口,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
实体类:
public class userlist { public string total { get; set; } public string count { get; set; } public userlistopenid data { get; set; } public string next_openid { get; set; } }
public class userlistopenid { public Listopenid { get; set; }
public class userdetail { public int subscribe { get; set; } public string openid { get; set; } public string nickname { get; set; } public int sex { get; set; } public string language { get; set; } public string city { get; set; } public string province { get; set; } public string country { get; set; } public string headimgurl { get; set; } public int subscribe_time { get; set; } public string unionid { get; set; } public string remark { get; set; } public int groupid { get; set; } public int[] tagid_list { get; set; } }
getUser.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="getUser.aspx.cs" Inherits="MyTest.WebUI.Manager.usermsg.getUser" %>
public partial class getUser : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //获取用户列表 protected void btnGet_Click(object sender, EventArgs e) { string next_opid = string.Empty; string url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token="+mainArg.get_Token()+"&next_openid="; HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) { StreamReader sr = new StreamReader(response.GetResponseStream()); string result = sr.ReadToEnd(); sr.Close(); MyTest.Common.Entity.userlist userlist = MyTest.Common.Util.JsonEntityExchange.Json2Entity(result); //Response.Write(userlist.count + "/"+userlist.data+"/"+userlist.next_openid+"/"+userlist.total); ddlopenids.DataSource = userlist.data.openid; ddlopenids.DataTextField = ""; ddlopenids.DataValueField = ""; ddlopenids.DataBind(); ListItem item = new ListItem(); item.Text = "--请选择用户的openID--"; item.Value = "--0--"; ddlopenids.Items.Insert(0, item); } } //获取用户基本信息(包括UnionID机制) protected void ddlopenids_SelectedIndexChanged(object sender, EventArgs e) { string url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+ mainArg.get_Token() + "&openid="+ddlopenids.SelectedItem.Text+"&lang=zh_CN "; HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) { StreamReader sr = new StreamReader(response.GetResponseStream()); string result = sr.ReadToEnd(); sr.Close(); MyTest.Common.Entity.userdetail user= MyTest.Common.Util.JsonEntityExchange .Json2Entity(result); lblMSG.Text = user.subscribe + "/" + user.openid + "/" + user.nickname + "/"; imgHead.ImageUrl = user.headimgurl; } } }
mainArg.cs获取accessToken帮助类:
public class mainArg { //测试号 public static string appid = "wx3eb5bf1290db2ca0"; public static string secret = "e6013be0a7338c7d3e02877db116e231"; public string jsapi_ticket; public string noncestr; public long timestamp; public string signature; private static string path = HttpContext.Current.Server.MapPath(@"~/TemplePath"); private static string tokenPath = HttpContext.Current.Server.MapPath(@"~/TemplePath/token.txt"); private static string ticketPath = HttpContext.Current.Server.MapPath(@"~/TemplePath/ticket.txt"); public mainArg() { noncestr = getNonceStr(); timestamp = getTime(); } ////// 获取access_token /// ///public static string get_Token() { string token = null; //判断是否存在或过期 if (File.Exists(tokenPath)) { FileStream fs = new FileStream(tokenPath, FileMode.Open); var serializer = new DataContractJsonSerializer(typeof(AccToken)); AccToken readJSToken = (AccToken)serializer.ReadObject(fs); fs.Close(); FileInfo fi = new FileInfo(tokenPath); if (CheckTimeOut(fi.LastWriteTime) < (readJSToken.expires_in-200)) { return token = readJSToken.access_token; } } string url = @"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&"; string urlarg = @"appid=" + appid + @"&secret=" + secret; // HttpUtility.UrlEncode(appid,Encoding.GetEncoding("utf-8")); url += urlarg; HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); using (WebResponse response = req.GetResponse()) { Stream s = response.GetResponseStream(); StreamReader sr = new StreamReader(s); token = sr.ReadToEnd(); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } if (File.Exists(tokenPath)) { File.Delete(tokenPath); } FileStream fs = File.Create(tokenPath); StreamWriter sw = new StreamWriter(fs); sw.Write(token); sw.Flush(); sw.Close(); fs.Close(); FileStream fs1 = new FileStream(tokenPath, FileMode.Open); var serializer = new DataContractJsonSerializer(typeof(AccToken)); AccToken readJSToken = (AccToken)serializer.ReadObject(fs1); fs1.Close(); token = readJSToken.access_token; return token; } } /// /// 获取ticket /// ///public string getTicket() { string ticket = null; // 判断是否存在或过期 if (File.Exists(ticketPath)) { FileStream fs = new FileStream(ticketPath, FileMode.Open); var serializer = new DataContractJsonSerializer(typeof(JsTicket)); JsTicket readJSTicket = (JsTicket)serializer.ReadObject(fs); fs.Close(); FileInfo fi = new FileInfo(ticketPath); if (CheckTimeOut(fi.LastWriteTime) < (readJSTicket.expires_in - 200)) { return ticket = readJSTicket.ticket; } } string url = @"https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&"; string urlarg = @"access_token="+get_Token(); // HttpUtility.UrlEncode(appid,Encoding.GetEncoding("utf -8")); url += urlarg; HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); using (WebResponse response = req.GetResponse()) { Stream s = response.GetResponseStream(); StreamReader sr = new StreamReader(s); ticket = sr.ReadToEnd(); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } if (File.Exists(ticketPath)) { File.Delete(ticketPath); } FileStream fs = File.Create(ticketPath); StreamWriter sw = new StreamWriter(fs); sw.Write(ticket); sw.Flush(); sw.Close(); fs.Close(); FileStream fs1 = new FileStream(ticketPath, FileMode.Open); var serializer = new DataContractJsonSerializer(typeof(JsTicket)); JsTicket readJSTicket = (JsTicket)serializer.ReadObject(fs1); fs1.Close(); ticket = readJSTicket.ticket; return ticket; } } // public static long getTime() { return Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds); } public static string getTimeString(DateTime dt) { TimeSpan ts = dt.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds).ToString(); } //获取16位随机字符串 public static string getNonceStr() { int length = 16; string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; string str = ""; Random rad = new Random(); for (int i = 0; i < length; i++) { str += chars.Substring(rad.Next(0, chars.Length - 1), 1); } return str; } //得到签名 public string getSign() { jsapi_ticket = getTicket(); string s1 = string.Format("jsapi_ticket={0}&noncestr={1}×tamp={2}&url=http://winsee.imwork.net/Manager/Main/testjs.aspx", jsapi_ticket, noncestr, timestamp.ToString()); signature = GetSHA1(s1); return signature; } public static string GetSHA1(string strSource) { string strResult = string.Empty; System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create(); byte[] bytResult = sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strSource)); for (int i = 0; i < bytResult.Length; i++) { strResult = strResult + bytResult[i].ToString("x2"); } return strResult; } //SHA1哈希加密算法 public static string GetSHA1_1(string str_sha1_in) { SHA1 sha1 = new SHA1CryptoServiceProvider(); byte[] bytes_sha1_in = Encoding.Default.GetBytes(str_sha1_in); byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in); string str_sha1_out = BitConverter.ToString(bytes_sha1_out); str_sha1_out = str_sha1_out.Replace("-", "").ToLower(); return str_sha1_out; } // 判断是否超过7200s public static long CheckTimeOut(DateTime changeTime) { return Convert.ToInt64((DateTime.Now - changeTime).TotalSeconds); } } # region 创建Json序列化 及反序列化类目 //创建JSon类 保存文件 ticket.txt public class AccToken { public string access_token { get; set; } public long expires_in { get; set; } } //创建从微信返回结果的一个类 用于获取ticket public class JsTicket { public long errcode { get; set; } public string errmsg { get; set; } public string ticket { get; set; } public long expires_in { get; set; } } #endregion
JSon序列化,反序列化
public class JsonEntityExchangewhere T:new() { /// /// json转实体LIST /// /// ///public static List Json2Entitys(string JsonStr) { JavaScriptSerializer Serializer = new JavaScriptSerializer(); List objs = Serializer.Deserialize >(JsonStr); return objs; } ///
/// json转实体 /// /// ///public static T Json2Entity(string json) { T obj = Activator.CreateInstance (); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); return (T)serializer.ReadObject(ms); } } /// /// 实体转json /// /// 实体list ///public static string Entity2Json(List lists) { return new JavaScriptSerializer().Serialize(lists); } }
结果如图:
关于如何使用.NET查看微信公众号关注者接口问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。
实体
序列
用户
问题
帮助
公众
接口
更多
结果
解答
易行
简单易行
代码
信息
内容
字符
字符串
小伙
小伙伴
文件
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库中NULL如何处理
服务器怎么做一键还原
力控数据库裸数据上限
邮局 系统 服务器
宝可梦兑换码服务器
怎样连接阿里云数据库
网络安全未来展望
软件开发模式和模型
SDE软件开发
嘉定区信息软件开发诚信服务
服务器主板型号大全详细图解
上海软件开发公司电话
安徽通用软件开发厂家价格
失落的方舟一直服务器验证失败
深圳达内软件开发学院
武汉大学网络安全学院研究生宿舍
服务器cpu最高安全温度
襄州区软件开发培训机构
服务器老是蓝屏怎么回事
c中数据库两个表关联
松江区创新数据库服务报价行情
金蝶10数据库兼容性
qq登陆服务器连接中稍后再试
讯飞mysql数据库工程师
常用数据库接口技术
40台锐起无盘服务器怎么设置
网络技术是国之重器对不对
江苏省网络安全测评中心
web工程用什么软件开发
软件开发笔试写代码满分