Asp. NET WeChat public number view fan information interface

Source: Internet
Author: User
Tags array to string openid
The example of this article for you to share the ASP. NET fan Information interface View Code, for your reference, the specific content is as follows

Token entity class:

<SUMMARY>///token entity class///</summary>public class Wechattokenentity{public string Access_token {get; set; public string expires_in {get; set;}}

User Information entity class

<SUMMARY>///User Entity Information class///</summary>public class Wechatuserentity{public string Subscribe {get; set;} Publ IC string Openid {get; set} public string Nickname {get; set;} public string Sex {get; set;} public string City {g Et Set The public string province {get, set,} public string Country {get, set,} public string Headimgurl {get, set;} public S Tring Subscribe_time {get; set;} public string Language {get; set;}}

Auxiliary Operation class

public class wechatdemo{/* * steps: * 1. Get token * 2 via AppID and secret request URL. User information via Access_token and OpenID (avatar address, etc.) * 3. By Access_ Token and media_id get the message sent by the user * */String appId = "Wxxxxxxxxxxxxxx"; string appsecret = "1234567890-==687"; String Wechaturl = "Https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret  ={1} "; Public Wechatdemo () {}//<summary>///get token information///</summary>///<returns></returns>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 from execution URL byte[] pagedata = client. Downloaddata (Tokenurl); Convert the original data byte array to string jsonstr = Encoding.Default.GetString (pagedata); Initialize a JavaScriptSerializer JSON parser//Serialization Note: You need to reference System.Web.Extensions JavaScriptSerializer JSS = new JavaScriptSerializer (); Deserializes a string into a token object MyToken = JSS. Deserialize<wechattokenentity> (JSONSTR);} catch (WebException ex) {throw ex;} catch (Exception ex) {throw ex;} return MyToken;} <summary>///Get user Information///</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?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<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?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;} <summary>///timestamp to current time///</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);} }

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 ("First step: Get access_token:\n" + token + "\ n"); Console.WriteLine ("Second step: Obtaining 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 ("Subscription:" + user.) Subscribe); Console.WriteLine ("Time:" + WCD. Timestamp2datetime (user. Subscribe_time)); Console.WriteLine ("Avatar Address:" + user.)  Headimgurl); Console.WriteLine ("Step Three: Get voice address");  String mediaid = "VWVNSKVSLDKVMSDLVKMDSLKVMSLD"; String voiceaddress = Wcd. Getvoice (token, mediaid); Console.WriteLine ("Voice Address:" + voiceaddress); Console.reAD ();}} 

Run results

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.