相思资源网 Design By www.200059.com

最近做微信公共号的开发,有个需求是这样的消息推送,以文本的形式把编辑的消息发送给微信企业号中的某一个应用组,这里做下笔记,以下是整理内容:

//定义数据模型
 
public class Access_token
{
public Access_token()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
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 = "应用组的CorpID";//在设置-》权限管理-》系统管理组
string AppSecret = "应用组的Secret";//在设置-》权限管理-》系统管理组
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;
}
 
///
 

根据当前日期 判断Access_Token 是否超期 如果超期返回新的Access_Token 否则返回之前的Access_Token

public static string IsExistAccess_Token()
{
string Token = string.Empty;
DateTime YouXRQ;
string strPath = "../../weixin/XMLFile.xml";
// 读取XML文件中的数据,并显示出来
//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 = "项目名称:"+来保网+""
}
};
 
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" + 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;
 
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

标签:
微信开发,消息推送,微信开发,消息推送实例,微信开发,消息推送实现代码

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“微信开发 消息推送实现代码”

暂无微信开发 消息推送实现代码的评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。