ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

微信公众号之发送微信模板--.net core 5.0

2022-05-20 11:03:51  阅读:36  来源: 互联网

标签:5.0 core string 微信 token new null response


static public string Template(string openId, string studentTel, string studentName, string source)
{
//从配置中提取后台域名
var apolloData = ApolloToConfigure.Get();
var siteUrl = apolloData.AdminMobileUrl;
var model = new TemplateMessage();
model.template_id = "模板id";
//页面跳转链接url
model.url = siteUrl;
model.topcolor = "#333";
model.touser = openId;
var name = string.IsNullOrEmpty(studentName) ? studentTel : studentName;

model.data = new
{
first = new
{
value = "你好!欢迎注册",
color = "#173177"
},
keyword1 = new//提交时间
{
value =$"注册时间["+ DateTime.Now.ToString("yyyy-MM-dd HH:mm")+"]",
color = "#173177"
},
keyword2 = new//提交内容
{
value = $"名称[" + name + "]通过[" + source + "]注册",
color = "#173177"
},
keyword3 = new//处理结果
{
value = $"注册成功!",
color = "#173177"
},
remark = new
{
value = $"请及时分配处理",
color = "#173177"
}
};
return Newtonsoft.Json.JsonConvert.SerializeObject(model);
}


/// <summary>
/// 发送微信消息
/// </summary>
/// <returns></returns>
public Tuple<bool, string> SendWeChatMessage(string message, string title)
{
try
{
var _access_token = Get_Access_token();
var response = Send_Message(message, _access_token);
//_logger.Info(response.errcode == "0" ? title + "微信消息发送成功" : title + "微信消息发送失败", response.errmsg);
if (response.errcode == "0") return Tuple.Create(true, "微信消息发送成功");
return Tuple.Create(false, response.errmsg);

}
catch (Exception ex)
{
_logger.Error(title + "微信消息发送失败", ex.Message);
return Tuple.Create(false, ex.Message);
}
}

/// <summary>
/// 微信消息发送URL
/// </summary>
protected string GetSendUrl
{
get
{
return "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
}
}


/// <summary>
/// 同步发送微信消息
/// </summary>
/// <param name="messages">消息内容(JSON字符串)</param>
protected ResultMsg Send_Message(string messages, string _access_token = null)
{
if (string.IsNullOrEmpty(_access_token))
{
_access_token = Get_Access_token();
}
var _sendUrl = GetSendUrl + _access_token;
var results = WebServiceApi.PostServer(_sendUrl, messages);
return Newtonsoft.Json.JsonConvert.DeserializeObject<ResultMsg>(results);
}

//发送消息到服务器
/// <summary>
/// Post请求(同步)
/// </summary>
/// <param name="posturl">URL</param>
/// <param name="postData">请求数据</param>
/// <returns></returns>
public static string PostServer(string posturl, string postData)
{
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
Encoding encoding = Encoding.UTF8;
byte[] data = encoding.GetBytes(postData);
// 准备请求...
try
{
// 设置参数
HttpWebRequest request = WebRequest.Create(posturl) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
request.ContentType = "text/xml;charset=utf-8";// "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
outstream = request.GetRequestStream();
outstream.Write(data, 0, data.Length);
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
//Response.Write(content);
return content;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (outstream != null)
{
outstream.Dispose();
outstream.Close();
}
if (instream != null)
{
instream.Dispose();
instream.Close();
}
if (sr != null)
{
sr.Dispose();
sr.Close();
}
if (response != null)
{
response.Dispose();
response.Close();
}

}
}

标签:5.0,core,string,微信,token,new,null,response
来源: https://www.cnblogs.com/wang150601/p/16291558.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有