/// <summary>
/// 向用户发送消息
/// </summary>
/// <param name="openid"></param>
/// <param name="contentText"></param>
private void send(string openid, string contentText)
{
Hashtable where = new Hashtable();
where.Add("access_token", access_token());
string url = "https://api.weixin.qq.com/cgi-bin/message/custom/send";
//定义JSON数据
JObject content = new JObject();
content.Add("content", contentText);
JObject j = new JObject();
j.Add("touser", openid);
j.Add("text", content);
j.Add("msgtype", "text");
string result = Util.HttpRequestPostBody(url, where, j.ToString());
throwErrmsg(result);
}
/// <summary>
/// 抛出微信异常结果
/// </summary>
/// <param name="result"></param>
private void throwErrmsg(string result)
{
Hashtable hr = JsonConvert.DeserializeObject<Hashtable>(result);
if (hr["errcode"].ToString().ToInt32() != 0)
{
throw new Exception(hr["errmsg"].ToString());
}
}
这里调用到的函数可以查看 微信公众号的开发之 自定义菜单(二) 这篇日志