国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

微信開發(fā)消息推送實(shí)現(xiàn)技巧(附代碼)

高洛峰
發(fā)布: 2017-03-25 09:58:40
原創(chuàng)
3105人瀏覽過

這篇文章主要介紹了微信開發(fā)消息推送實(shí)現(xiàn)技巧(附代碼) 消息推送實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下

最近做微信公共號的開發(fā),有個(gè)需求是這樣的消息推送,以文本的形式把編輯的消息發(fā)送給微信企業(yè)號中的某一個(gè)應(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; }
}
 
 
///
 
 
/// 憑證有效時(shí)間,單位:秒
///
 
public string expires_in
{
get { return _expires_in; }
 
 
set { _expires_in = value; }
}
 
}
登錄后復(fù)制
 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;
}
 
///
登錄后復(fù)制

根據(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 = "項(xiàng)目名稱:"+來保網(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;
 
}
登錄后復(fù)制

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章:

一個(gè)基于WebSocket的WEB消息推送框架

.NET 微信開發(fā)自動(dòng)內(nèi)容回復(fù)實(shí)例代碼

在Java中通過websocket實(shí)現(xiàn)消息推送的實(shí)現(xiàn)代碼詳解

以上就是微信開發(fā)消息推送實(shí)現(xiàn)技巧(附代碼)的詳細(xì)內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!

微信app下載
微信app下載

微信是一款手機(jī)通信軟件,支持通過手機(jī)網(wǎng)絡(luò)發(fā)送語音短信、視頻、圖片和文字。微信可以單聊及群聊,還能根據(jù)地理位置找到附近的人,帶給大家全新的移動(dòng)溝通體驗(yàn),有需要的小伙伴快來保存下載體驗(yàn)吧!

下載
來源:php中文網(wǎng)
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn
最新問題
開源免費(fèi)商場系統(tǒng)廣告
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號