C# 小程序订阅消息功能开发:
1.管理员账户登录 2.点击左侧菜单栏订阅消息,申请或选用模板 3.通过申请模板得到模板ID,后续开发要使用。 如下图所示: 4.点击详情,查看模板关键字,后续开发会使用 如下图所示 5.后端代码 post请求代码
public static string HttpPostRequest(string path, string parameter)
{
string Jsonresult = string.Empty;
try
{
byte[] b = Encoding.UTF8.GetBytes(parameter);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = b.Length;
using (Stream s = request.GetRequestStream())
{
s.Write(b, 0, b.Length);
s.Close();
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
Jsonresult = sr.ReadToEnd().Trim();
response.Close();
sr.Close();
}
}
JObject jo = (JObject)JsonConvert.DeserializeObject(Jsonresult);
return jo["errcode"].ToString();
}
catch
{
return "1";
}
}
发送订阅代码
public string WxNotification(string OpenID, string template_id, string keyword1 = "", string keyword2 = "")
{
string httpResult = "";
if (keyword1.Length > 20 || keyword2.Length > 20)
{
httpResult = "1";
}
var param = new
{
touser = OpenID,
template_id = template_id,
data = new
{
thing1 = new { value = keyword1 },
thing4 = new { value = keyword2 }
}
};
httpResult = HttpPostRequest("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + AdminUtil.GetAccessToken(), JsonConvert.SerializeObject(param));
return httpResult;
}
获取微信token方法,GetAccessToken();这里就不贴了,网上很多,通过Appid(小程序ID)和secret(密钥)去获取即可。
|