1.网上收集了代码能用
public class AccessToken
{
[JsonProperty(PropertyName = "access_token")]
public string Accesstoken { get; set; }
[JsonProperty(PropertyName = "expires_in")]
public int ExpiresIn { get; set; }
}
public ActionResult Index()
{
AccessToken at = wxApi.GetToken("wx0b024f521540d3", "0bfcdde311c42067a1ece9eb2aa7ef48");
///发送模版消息https://www.cnblogs.com/amoshu/p/4988365.html
string url = string.Format("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}", at.Accesstoken);
string strPostData = "{ \"touser\":\"" + "ohqkK02jdYvlM2idMlHkRfL5HBwg" + "\",\"template_id\":\"i0a6OryTmmH-NMcJ6S2Jm6iSbRk_4s80WfLWELyt1cc\",\"url\":\"http://www.woizuqiu.com/Home/Index\", \"data\":{\"first\":{\"value\":\"微信\",\"color\":\"#173177\"},\"productType\":{\"value\":\"113234\",\"color\":\"#173177\"},\"name\":{\"value\":\"1\",\"color\":\"#173177\"},\"result\":{\"value\":\"2\",\"color\":\"#173177\"},\"remark\":{\"value\":\"3\",\"color\":\"#173177\"}}}";
string strJson = ClassLibrary1.wxApi.HttpPostData(url, strPostData);
return View();
}
public static string HttpPostData(string posturl, string postData)
{
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = Encoding.UTF8;
byte[] data = encoding.GetBytes(postData);
// 准备请求...
try
{
// 设置参数
request = WebRequest.Create(posturl) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
outstream = request.GetRequestStream();
outstream.Write(data, 0, data.Length);
outstream.Close();
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
string err = string.Empty;
//Response.Write(content);
return content;
}
catch (Exception ex)
{
string err = ex.Message;
return string.Empty;
}
}
2.整理记录集成到项目中
网友评论