Recently do the development of micro-letter public number, there is a demand is such a message push, in the form of text to send the edited message to the micro-letter enterprise number of an application group, here to take notes, the following is the collation of content:
Define data Model public
class Access_token
{public
Access_token ()
{////
TODO: Add constructor logic here
//
}
string _access_token;
string _expires_in;
///Get the voucher
///public
string Access_token
{getting
{return _access_token;}
set {_access_token = value;}
}
///Voucher Valid time, in units: seconds
///public
string expires_in
{get {return
_expires_in;}
set {_expires_in = value;}
}
}
Public ActionResult Index (string returnurl)
{
getaccess_token ();
Isexistaccess_token ();
View ();
}
public static Access_token Getaccess_token ()
{
string appurl = ' Https://qyapi.weixin.qq.com/cgi-bin/gettoken ?";
String AppID = "Corpid of application Group";//In Settings-rights Management-system Management Group
string appsecret = "Secret of application Group";/In Settings-"Rights Management-" System Management Group
WebClient WebClient = new WebClient ();
byte[] bytes = Webclient.downloaddata (string. Format ("{0}corpid={1}&corpsecret={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
}
///
According to the current date to determine whether Access_token extended if the extended return to the new Access_token otherwise return before the Access_token
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:" + to Protect Net + "}};
String wcr= btnsend (Token, text);
return WCR; public static string Btnsend (string Token, Object text) {string url = ' Https://qyapi.weixin.qq.com/cgi-bin/message/se
Nd?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, I hope to help you, thank you for your support for this site!