using System;
System.IO;
System.Net;
System.Text;
Newtonsoft.Json;
namespace JJHL.Service
{
/// <summary>
/// 微信消息推送
</summary>
public class WxChatPrompt
{
public WxChatPrompt()
{
}
private static WxChatPrompt _objPrompt;
WxChatPrompt _
{
get => _objPrompt ?? new WxChatPrompt();
set => _objPrompt = value;
}
<summary>
消息推送
</summary>
<param name="Access_token">网页授权凭证,通过微信接口获取</param>
<param name="Openid">要推送的用户oppenid<returns></returns>
string MsgPush(string Access_token,string Openid)
{
string templateId = "";//模板编号
string firstContent= 内容
string keyword1 = 自定义内容
string keyword2 = string keyword3 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); //时间
string remark = 备注
string contentmsg = {\"touser\":\"" + Openid + \",\"template_id\":\"" + templateId + " + firstContent + " + keyword1 + " + keyword2 + " + keyword3 + " +remark + ";
string result = WeChatPushNotice(Access_token,contentmsg);
return result;
}
微信消息推送
<param name="accessToken">微信access_token<param name="contentMsg">推送内容string WeChatPushNotice(string accessToken,1)"> contentMsg)
{
string promat = ""需要提交的数据
byte[] bs = Encoding.UTF8.GetBytes(contentMsg);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken + );
req.Method = POST;
req.ContentType = application/x-www-form-urlencoded;
req.ContentLength = bs.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs,0,bs.Length);
}
HttpWebResponse respon = (HttpWebResponse)req.GetResponse();
Stream stream = respon.GetResponseStream();
using (StreamReader reader = StreamReader(stream,Encoding.UTF8))
{
promat = reader.ReadToEnd();
}
ReturnMsg y = JsonConvert.DeserializeObject<ReturnMsg>(promat);
promat = y.errmsg;
promat;
}
自定义模型
</summary>
ReturnMsg
{
string errcode { get; set; }
string errmsg { string msgid { ; }
}
}
}
{
errcode":errmsgokmsgid200228332
}