In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk with you about how to use the official account of ASP.NET Wechat to view the fan information interface, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something from this article.
Wechat Token entity class:
/ / Wechat Token entity class / public class WeChatTokenEntity {public string Access_token {get; set;} public string Expires_in {get; set;}}
User information entity class
/ / 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;}
Wechat auxiliary operation class
Public class WeChatDemo {/ * * step: * 1. Request Wechat url through appid and secret to get token * 2. Get user information (avatar address, etc.) through access_token and openid. Get the Wechat message sent by users through access_token and media_id * * / string appId = "wxxxxxxxxxxxxxx"; string appSecret = "1234567890"; string wechatUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";" Public WeChatDemo () {} / get token information / public WeChatTokenEntity GetWechatToken () {/ / requested url address string tokenUrl = string.Format (wechatUrl, appId, appSecret); WeChatTokenEntity myToken; try {/ / declare and instantiate a WebClient object WebClient client = new WebClient (); / / download data byte from executing url [] pageData = client.DownloadData (tokenUrl) / / convert the byte array of raw data to the string string jsonStr = Encoding.Default.GetString (pageData); / / initialize a JavaScriptSerializer json parser / / serialize note: reference System.Web.Extensions JavaScriptSerializer jss = new JavaScriptSerializer (); / / deserialize the string into Token object myToken = jss.Deserialize (jsonStr);} catch (WebException ex) {throw ex;} catch (Exception ex) {throw ex;} return myToken } / get user information / public WeChatUserEntity GetUserIfo (string accessToken, string openId) {WeChatUserEntity wue = new WeChatUserEntity (); string url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}"; 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 (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?access_token={0}&media_id={1}"; voiceUrl = string.Format (voiceUrl, accessToken, mediaId); WebClient wc = new WebClient (); byte [] pageData = wc.DownloadData (voiceUrl); string jsonStr = Encoding.UTF8.GetString (pageData); / / TODO: get sound voiceAddress = jsonStr; return voiceAddress } / 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);}}
Main program:
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 ("step 1: get access_token:\ n" + token + "\ n"); Console.WriteLine ("step 2: get user information"); WeChatUserEntity user = wcd.GetUserIfo (token, openId); Console.WriteLine ("\ n nickname:" + user.Nickname) Console.WriteLine ("country:" + user.Country); Console.WriteLine ("Province:" + user.Province); Console.WriteLine ("City:" + user.City); Console.WriteLine ("language:" + user.Language); Console.WriteLine ("gender:" + user.Sex); Console.WriteLine ("OpenId:" + user.Openid); Console.WriteLine ("whether to subscribe:" + user.Subscribe) Console.WriteLine ("time:" + wcd.TimeStamp2DateTime (user.Subscribe_time)); Console.WriteLine ("avatar address:" + user.HeadImgUrl); Console.WriteLine ("\ n3rd step: get Wechat voice address"); string mediaId = "vwvnskvsldkvmsdlvkmdslkvmsld"; string voiceAddress = wcd.GetVoice (token, mediaId); Console.WriteLine ("voice address:" + voiceAddress); Console.Read ();}}
The running result is shown in the figure:
After reading the above, do you have any further understanding of how to use the ASP.NET Wechat official account to view fan information interface? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.