using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Net; using System.Text; namespace Basic.Tools { /// /// 发送模板消息通知会员 /// public class SendInfo { public static void SendTheInfo(string type, string openid, string appid, string secret, string strContent) { string access_token = GetResult("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret); access_token = access_token.Remove(0, access_token.IndexOf("\":\"")).Split('"')[2]; string url = string.Format("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}", access_token); string content = ""; string[] ArrayCon = strContent.Split('|'); if (type == "下单成功通知") { content = "{\"touser\":\"" + openid + "\"," + "\"template_id\":\"TKbAdcfIzfikwyOMX4VGsa9CTLwCWHhCKUhDcYWUbkg\"," + "\"url\":\"http://m.ivhua.com/\"," + "\"topcolor\":\"#d4a93a\"," + "\"data\":{" + "\"first\": {" + "\"value\":\"" + ArrayCon[0] + "\"," + "\"color\":\"#0A0A0A\"" + "}," + "\"keyword1\": {" + "\"value\":\"" + ArrayCon[1] + "\"," + "\"color\":\"#0A0A0A\"" + "}," + "\"keyword2\":{" + "\"value\":\"" + ArrayCon[2] + "\"," + "\"color\":\"#0A0A0A\"" + "}," + "\"keyword3\": {" + "\"value\":\"" + ArrayCon[3] + "\"," + "\"color\":\"#0A0A0A\"" + "}," + "\"keyword4\": {" + "\"value\":\"" + ArrayCon[4] + "\"," + "\"color\":\"#0A0A0A\"" + "}," + "\"keyword5\": {" + "\"value\":\"" + ArrayCon[5] + "\"," + "\"color\":\"#0A0A0A\"" + "}," + "\"remark\": {" + "\"value\":\"" + ArrayCon[6] + "\"," + "\"color\":\"#173177\"" + "} " + "}" + "}"; } if (type == "订单发货通知") { content = "{\"touser\":\"" + openid + "\"," + "\"template_id\":\"q6-u6t8YK0e1m3MYLIfJdR5LOuqC0jCrbd7QXWADSW4\"," + "\"url\":\"http://m.ivhua.com/\"," + "\"topcolor\":\"#d4a93a\"," + "\"data\":{" + "\"first\": {" + "\"value\":\"" + ArrayCon[0] + "\"," + "\"color\":\"#0A0A0A\"" + "}," + "\"keyword1\": {" + "\"value\":\"" + ArrayCon[1] + "\"," + "\"color\":\"#0A0A0A\"" + "}," + "\"keyword2\":{" + "\"value\":\"" + ArrayCon[2] + "\"," + "\"color\":\"#0A0A0A\"" + "}," + "\"keyword3\": {" + "\"value\":\"" + ArrayCon[3] + "\"," + "\"color\":\"#0A0A0A\"" + "}," + "\"keyword4\": {" + "\"value\":\"" + ArrayCon[4] + "\"," + "\"color\":\"#0A0A0A\"" + "}," + "\"keyword5\": {" + "\"value\":\"" + ArrayCon[5] + "\"," + "\"color\":\"#0A0A0A\"" + "}," + "\"remark\": {" + "\"value\":\"" + ArrayCon[6] + "\"," + "\"color\":\"#173177\"" + "} " + "}" + "}"; } SendPostHttpRequest(url, "application/x-www-form-urlencoded", content); } private static string GetResult(string theurl) { string postString = theurl.Split('?')[1];//这里即为传递的参数,可以用工具抓包分析,也可以自己分析,主要是form里面每一个name都要加进来 byte[] postData = Encoding.UTF8.GetBytes(postString);//编码,尤其是汉字,事先要看下抓取网页的编码方式 string url = theurl.Split('?')[0];//地址 WebClient webClient = new WebClient(); webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可 byte[] responseData = webClient.UploadData(url, "POST", postData);//得到返回字符流 string srcString = Encoding.UTF8.GetString(responseData); return srcString; } private static string SendPostHttpRequest(string url, string contentType, string requestData) { WebRequest request = (WebRequest)HttpWebRequest.Create(url); request.Method = "POST"; byte[] postBytes = null; request.ContentType = contentType; postBytes = Encoding.UTF8.GetBytes(requestData); request.ContentLength = postBytes.Length; using (Stream outstream = request.GetRequestStream()) { outstream.Write(postBytes, 0, postBytes.Length); } string result = string.Empty; using (WebResponse response = request.GetResponse()) { if (response != null) { using (Stream stream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } } } } return result; } } }