Asp. NET micro-Credit public number view fan Information interface _ practical Tips

Source: Internet
Author: User
Tags datetime openid

This example for you to share the ASP.net micro-letter fan information interface to see the code for your reference, the specific content as follows

Micro-Letter Token entity classes:

 <summary>
 ///Micro-Token entity class
 ///</summary> public
 class wechattokenentity
 {
 public String Access_token {get; set;}

 public string expires_in {get; set;}
 }

User Information entity classes

 <summary>
 ///User entity information class
 ///</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;}
 }

Micro-letter Auxiliary operation class

 public class Wechatdemo {* * * steps: * 1. AppID and secret request the micro-letter URL, get token * 2. Get user information (avatar address, etc.) via Access_token and OpenID *
 3. By Access_token and MEDIA_ID received 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 () {//URL address string tokenurl = string of the request.
  Format (Wechaturl, AppId, Appsecret);

  Wechattokenentity MyToken;
  try {//declare and instantiate a WebClient object WebClient client = new WebClient (); Download data from the execution URL byte[] pagedata = client.
  Downloaddata (Tokenurl);
  Convert the byte array of the original data into a string jsonstr = Encoding.Default.GetString (pagedata); Initializes a JavaScriptSerializer JSON parser//Serialization Note: You need to reference System.Web.Extensions JavaScriptSerializer JSS = new javascriptserial
  Izer (); WillThe string is deserialized to 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>///<par Am 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>///<re Turns></returns> Public DateTime timestamp2datetime (string timeStamp) {DateTime Dtstart = Timezone.currentti
  Mezone.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 ("Step Two: Obtain 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 ("Sex:" + user.)
  SEX); Console.WriteLine ("OpenId:" + user.)
  Openid); Console.WriteLine ("Subscribe to:" + user.)
  Subscribe); Console.WriteLine ("Time:" + WCD.) Timestamp2datetime (user.
  Subscribe_time)); Console.WriteLine ("Avatar Address:" + user.)

  Headimgurl);
  Console.WriteLine ("\ n Third step: obtaining a micro-letter voice address");

  String mediaId = "VWVNSKVSLDKVMSDLVKMDSLKVMSLD"; String voiceaddress = Wcd.
  Getvoice (token, mediaId); COnsole.
  WriteLine ("Voice address:" + voiceaddress);
 Console.read ();

 }
 }

Run the results as shown in figure:

This article has been sorted out to the ASP. NET micro-Credit Development tutorial Summary, you are welcome to learn to read.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.