. NET WeChat public account viewing receiver interface,. net mail public interface

Source: Internet
Author: User
Tags openid sha1 hash

. NET public account Viewer interface,. net mail public interface

The examples in this article share with you how to obtain different paths in java for your reference. The specific content is as follows:

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<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 code:

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "getUser. aspx. cs "Inherits =" MyTest. webUI. manager. usermsg. getUser "%> <! DOCTYPE html> 
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 <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 = "-- 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 <MyTest. common. entity. userdetail>. 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 ();} /// <summary> /// obtain access_token /// </summary> /// <returns> </returns> public static string get_Token () {string token = null; // determine 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 ;}/// <summary> /// get ticket /// </summary> /// <returns> </returns> 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 <T> where T: new () {// <summary> /// json to Object 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> /// convert json to object /// </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> /// convert the object to json /// </summary> /// <param name = "lists"> Object list </param> /// <returns> </returns> public static string Entity2Json (List <T> lists) {return new JavaScriptSerializer (). serialize (lists );}}

Result:

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.