如何使用.NET查看微信公眾號關注者接口,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
十年的班戈網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。全網(wǎng)整合營銷推廣的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調(diào)整班戈建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)公司從事“班戈網(wǎng)站設計”,“班戈網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。實體類:
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 List<string> openid { 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" %> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title></title> <!-- Bootstrap --> <link href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"> <!--[if lt IE 9]> <script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script> <![endif]--> </head> <body> <form runat="server"> <div class="container"> <div class="row"> <div class="col-md-6 col-md-push-2"> <asp:Button class="btn btn-primary" ID="btnGet" runat="server" Text="獲取所有用戶openID" OnClick="btnGet_Click" /> </div> <div class="col-md-6 col-md-pull-2"> <asp:DropDownList CssClass="form-control" ID="ddlopenids" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlopenids_SelectedIndexChanged"></asp:DropDownList> <asp:Label ID="lblMSG" runat="server" Text=""></asp:Label> <asp:Image class="img-circle" ID="imgHead" runat="server" Width="180px" Height="180px" /> </div> </div> </div> <script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script> <script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> </form> </body> </html>
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<MyTest.Common.Entity.userlist>.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<MyTest.Common.Entity.userdetail>.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(); } /// <summary> /// 獲取access_token /// </summary> /// <returns></returns> 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; } } /// <summary> /// 獲取ticket /// </summary> /// <returns></returns> 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 創(chuàng)建Json序列化 及反序列化類目 //創(chuàng)建JSon類 保存文件 ticket.txt public class AccToken { public string access_token { get; set; } public long expires_in { get; set; } } //創(chuàng)建從微信返回結(jié)果的一個類 用于獲取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 JsonEntityExchange<T> where T:new() { /// <summary> /// json轉(zhuǎn)實體LIST /// </summary> /// <param name="JsonStr"></param> /// <returns></returns> public static List<T> Json2Entitys(string JsonStr) { JavaScriptSerializer Serializer = new JavaScriptSerializer(); List<T> objs = Serializer.Deserialize<List<T>>(JsonStr); return objs; } /// <summary> /// json轉(zhuǎn)實體 /// </summary> /// <param name="json"></param> /// <returns></returns> public static T Json2Entity(string json) { T obj = Activator.CreateInstance<T>(); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); return (T)serializer.ReadObject(ms); } } /// <summary> /// 實體轉(zhuǎn)json /// </summary> /// <param name="lists">實體list</param> /// <returns></returns> public static string Entity2Json(List<T> lists) { return new JavaScriptSerializer().Serialize(lists); } }
結(jié)果如圖:
關于如何使用.NET查看微信公眾號關注者接口問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關知識。
網(wǎng)站名稱:如何使用.NET查看微信公眾號關注者接口-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://jinyejixie.com/article8/dsigop.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、服務器托管、自適應網(wǎng)站、電子商務、關鍵詞優(yōu)化、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容