Develop message pushing implementation code and code
I recently developed a public account and want to push messages like this. I sent the edited message to an application group in the enterprise account in the form of text. Here I will take a note, the following is the content:
// Define the data model public class Access_token {public Access_token () {// TODO: add the constructor logic here //} string _ access_token; string _ expires_in; /// // The obtained credential // public string access_token {get {return _ access_token;} set {_ access_token = value ;}} //// validity period of the credential, in the unit of seconds // public string expires_in {get {return _ expires_in ;}set {_ expires_in = value ;}}}
Public ActionResult index (string returnUrl) {GetAccess_token (); IsExistAccess_Token (); return View ();} public static Access_token GetAccess_token () {string AppUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken? "; String AppID =" CorpID of the Application Group "; // choose Settings> permission management> System Management Group string AppSecret =" Secret of the Application Group "; // choose Settings> permission management> System Management Group WebClient webClient = new WebClient (); Byte [] bytes = webClient. downloadData (string. format ("{0} corpid = {1} & Cortana cret = {2}", AppUrl, AppID, AppSecret); string result = Encoding. getEncoding ("UTF-8 "). getString (bytes); JObject jObj = JObject. parse (result); string token = jObj ["access_token"]. toString (); string expires_in = jObj ["expires_in"]. toString (); Access_token mode = new Access_token (); mode. access_token = token; mode. expires_in = expires_in; return mode ;}///
Determine whether the Access_Token is out of date based on the current date. If the new Access_Token is returned during the expiration period, the previous Access_Token is returned.
Public static string IsExistAccess_Token () {string Token = string. empty; DateTime YouXRQ; string strPath = ".. /.. /weixin/XMLFile. xml "; // read the data in the XML file and display it // string filepath = System. web. hosting. hostingEnvironment. mapPath (strPath); string filepath = System. IO. path. combine (AppDomain. currentDomain. baseDirectory, strPath); StreamReader str = new StreamReader (filepath, System. text. encoding. UTF8); XmlDocument Xml = new XmlDocument (); xml. load (str); str. close (); str. dispose (); Token = xml. selectSingleNode ("xml "). selectSingleNode ("Access_Token "). innerText; YouXRQ = Convert. toDateTime (xml. selectSingleNode ("xml "). selectSingleNode ("Access_YouXRQ "). innerText); if (DateTime. now> YouXRQ) {DateTime _ youxrq = DateTime. now; Access_token mode = GetAccess_token (); xml. selectSingleNode ("xml "). selectSingleNode ("Access_Token "). InnerText = mode. access_token; _ youxrq = _ youxrq. addSeconds (int. parse (mode. expires_in); xml. selectSingleNode ("xml "). selectSingleNode ("Access_YouXRQ "). innerText = _ youxrq. toString (); xml. save (filepath); Token = mode. access_token;} object text = new {toparty = "1", agentid = "2", msgtype = "text", text = new {content = "Project name: "+ laibao Network +" "}}; string wcr = btnSend (Token, text); return wcr;} public static string B TnSend (string Token, object text) {string url = "https://qyapi.weixin.qq.com/cgi-bin/message/send? Access_token = "+ Token; WebRequest req = WebRequest. create (url); JavaScriptSerializer aa = new JavaScriptSerializer (); string postData = aa. serialize (text); byte [] requestBytes = Encoding. UTF8.GetBytes (postData); req. method = "POST"; req. contentType = "application/x-www-form-urlencoded"; req. contentLength = requestBytes. length; Stream requestStream = req. getRequestStream (); requestStream. write (requestBytes, 0, requestBytes. length); requestStream. close (); HttpWebResponse res = (HttpWebResponse) req. getResponse (); StreamReader sr = new StreamReader (res. getResponseStream (), Encoding. default); string backstr = sr. readToEnd (); sr. close (); res. close (); WeChatReturn WCR = aa. deserialize (backstr); return WCR. errmsg ;}
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!