前段时间开发了公司的微信
这里做个知识总结分享下经验,望一起学习。。。。。
感觉后面写个系列的最好了 。。。。
企业需求:
给指定企业用户发送消息:如考勤信息、 企业通知、个人信息推送等等,
///
/// 要发送的人ID
/// 消息
private void SendMessage(string UserID, string StrMessg)
{
Como.LogUtil.WriteLog("回复用户" + UserID + "消息");
string Access_Token = Como.GetAccessToken.GetAccess_token();
if (Access_Token == "")
Como.LogUtil.WriteException("SendMessage 未能成功加载Access_Token");
string Text = @"{
""touser"":";
Text += '"' + UserID + '"';
Text += "," + '"' + @"msgtype"": ""text"",
""agentid"": ""5"",
""text"": {
""content"":";
Text += '"' + StrMessg + '"';
Text += @"},
""safe"": ""0""
}";
;
string url = String.Format("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}", Access_Token);
string strResult = Como.Tools.GetPage(url, Text);
JArray ja = (JArray)JsonConvert.DeserializeObject("[" + strResult + "]");
string Error = "";
try
{
if (strResult.Contains("errcode"))
{
Error = ja[0]["errcode"].ToString();
}
}
catch (Exception ex)
{
Como.LogUtil.WriteException("获取strResult,未获取到错误信息" + ex.Message.ToString());
}
string errcode = ja[0]["errcode"].ToString();
string errmsg = ja[0]["errmsg"].ToString();
if (errcode == "0" && errmsg == "ok")
{
Como.LogUtil.WriteLog("回复成功!");
}
else
{
Como.LogUtil.WriteLog("回复失败!");
Como.LogUtil.WriteException("回复失败:SendMessage:" + strResult);
}
}////// 根据请求地址 和数据返回请求结果 /// /// /// ///public static string GetPage(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; return content; } catch (Exception ex) { string err = ex.Message; Como.LogUtil.WriteException("Tools GetPage 异常为:"+err.ToString()); return string.Empty; } }
参数引用详解:
1.Como.GetAccessToken.GetAccess_token() //获取 Access_token
微信分销商城电脑手机三合一是以php+MySQL进行开发的微信商城分销系统源码。安装步骤:1、打开:网址/diguo/index.php 用户密码是admin 123456 登录进去配置数据库信息。2、用帝国还原恢复数据库.3、修改data文件夹里的config.php (data/config.php)数据库配置信息4、登录网站后台,网址:域名/admin/index.php 后台帐号是:
2.GetPage //返回页面请求信息 结果
以上为基础发送关键代码:
以下为:成功后的实例图:











