This article mainly gives you a detailed description of the. NET public account, which has some reference value. if you are interested, you can refer to the entity class:
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
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 code:
<%@ 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) {}// obtain the user list 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 = 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 = "-- select the user's openID --"; item. value = "-- 0 --"; ddlopenids. items. insert (0, item) ;}/// obtain basic user information (including the UnionID mechanism) 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:
Public class mainArg {// Test No. public static string appid = "wx3eb5bf1290db2ca0"; public static string secret = "secret"; 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 ();}////// Obtain access_token //////
Public static string get_Token () {string token = null; // determines whether the object exists or expires 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 ;}}////// Get ticket //////
Public string getTicket () {string ticket = null; // determines whether the request exists or expires 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 )). 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 () ;}// obtain the 16-bit random string public static string getNonceStr () {int length = 16; string chars = "character"; 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;} // obtain the signature public string getSign () {jsapi_ticket = getTicket (); string s1 = string. format ("jsapi_ticket = {0} & noncestr = {1} & timestamp = {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 hash encryption algorithm 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;} // checks whether the value exceeds 7200 s public static long CheckTimeOut (DateTime changeTime) {return Convert. toInt64 (DateTime. now-changeTime ). totalSeconds) ;}# region creates a Json serialization and deserialization category // creates a JSon class to save the file ticket.txt public class AccToken {public string access_token {get; set ;} public long expires_in {get; set ;}// create a class from the returned result to get 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 serialization and deserialization
Public class JsonEntityExchange
Where T: new (){///
/// Json to object LIST //////
///
Public static List
Json2Entitys (string JsonStr) {JavaScriptSerializer Serializer = new JavaScriptSerializer (); List
Objs = Serializer. Deserialize
> (JsonStr); return objs ;}///
/// Convert json to object //////
///
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 );}}///
/// Convert an object to json //////
Entity list///
Public static string Entity2Json (List
Lists) {return new JavaScriptSerializer (). Serialize (lists );}}
Result:
The above is the details of the. NET instance tutorial for viewing the handler interface on the public account. For more information, see other related articles on php Chinese network!