微信開發(fā) 消息推送實現(xiàn)代碼
最近做微信公共號的開發(fā),有個需求是這樣的消息推送,以文本的形式把編輯的消息發(fā)送給微信企業(yè)號中的某一個應(yīng)用組,這里做下筆記,以下是整理內(nèi)容:
//定義數(shù)據(jù)模型
public class Access_token
{
public Access_token()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
string _access_token;
string _expires_in;
///
/// 獲取到的憑證
///
public string access_token
{
get { return _access_token; }
set { _access_token = value; }
}
///
/// 憑證有效時間,單位:秒
///
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 = "應(yīng)用組的CorpID";//在設(shè)置-》權(quán)限管理-》系統(tǒng)管理組
string AppSecret = "應(yīng)用組的Secret";//在設(shè)置-》權(quán)限管理-》系統(tǒng)管理組
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;
}
///
根據(jù)當(dāng)前日期 判斷Access_Token 是否超期 如果超期返回新的Access_Token 否則返回之前的Access_Token
public static string IsExistAccess_Token()
{
string Token = string.Empty;
DateTime YouXRQ;
string strPath = "../../weixin/XMLFile.xml";
// 讀取XML文件中的數(shù)據(jù),并顯示出來
//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 = "項目名稱:"+來保網(wǎng)+""
}
};
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/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;
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
深入淺析JavaScript中的arguments對象(強力推薦)
這篇文章主要介紹了JavaScript中的arguments對象(強力推薦)的相關(guān)資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-06-06
使用PBFunc在Powerbuilder中支付寶當(dāng)面付款功能
這篇文章主要介紹了使用PBFunc在Powerbuilder中支付寶當(dāng)面付款功能的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-10-10
bootstrap daterangepicker雙日歷時間段選擇控件詳解
這篇文章主要為大家詳細(xì)介紹了bootstrap daterangepicker雙日歷時間段選擇控件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
Javascript實現(xiàn)簡易天數(shù)計算器
這篇文章主要為大家詳細(xì)介紹了Javascript實現(xiàn)簡易天數(shù)計算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05
JavaScript實現(xiàn)頁面動態(tài)驗證碼的實現(xiàn)示例
這篇文章主要介紹了JavaScript實現(xiàn)頁面動態(tài)驗證碼的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Layui給數(shù)據(jù)表格動態(tài)添加一行并跳轉(zhuǎn)到添加行所在頁的方法
今天小編就為大家分享一篇Layui給數(shù)據(jù)表格動態(tài)添加一行并跳轉(zhuǎn)到添加行所在頁的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08

