Example #1
0
        /// <summary>
        /// 给用户发送信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSend_Click(object sender, EventArgs e)
        {
            string[] select = PickUpSelectFriend();

            object user = select.Length > 0 ? select[0] : null;

            // object user = lstBoxUser.SelectedItem;
            if (user == null)
            {
                txtTip.Text = "请选择用户!";
                return;
            }

            if (string.IsNullOrEmpty(txtBoxMessage.Text.Trim()) && !ckAppendDateTime.Checked)
            {
                txtTip.Text = "请输入信息!";
                return;
            }
            //是否可以批量发送呢?



            //string userid = user.ToString().Substring(user.ToString().LastIndexOf('>')+1);

            //string userid = string.Join(",", select);// user.ToString();
            foreach (string item in select)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(txtBoxMessage.Text);
                if (ckAppendDateTime.Checked)
                {
                    sb.AppendLine(DateTime.Now.ToString(format));
                }
                SendMsgResponse result = SendMsg(item, user.ToString(), sb.ToString(), Uin, false);
                txtTip.Text += "\r\n" + result.BaseResponse.ErrMsg;
                // SendMsg(item, USER_INFO, sb.ToString(), false);
            }
            if (ckClearText.Checked)
            {
                txtBoxMessage.Text = "";
            }
        }
Example #2
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="FromUserName">接收者 不要搞错了</param>
        /// <param name="ToUserName">发送者</param>
        /// <param name="msg">消息</param>
        /// <param name="uin">生成验证码时提供的tocken</param>
        /// <param name="robot">true 机器人 false 普通用户</param>
        private SendMsgResponse SendMsg(string FromUserName, string ToUserName, string msg, string uin, bool robot = true)
        {
            //发消息
            var p = @"{""BaseRequest"":{""Uin"":{UIN},""Sid"":""{SID}"",""Skey"":""{SKEY}"",""DeviceID"":""{DeviceID}""},""Msg"":{""Type"":1,""Content"":""{msg}"",""FromUserName"":""{FromUserName}"",""ToUserName"":""{ToUserName}"",""LocalID"":""{LocalID}"",""ClientMsgId"":""{ClientMsgId}""},""Scene"":0}";

            /*{
             *  BaseRequest:{
             *      DeviceID:"e863342965379663"
             *      Sid:"2NHpeQJtywSJQX13"
             *      Skey:"@crypt_45ae67ad_df25cf6d28dc1392c220237ac91441b8"
             *      Uin:2266323382
             *  }
             *  Msg:{
             *      ClientMsgId:"15282968813090945"
             *      Content:"刚刚试的"
             *      FromUserName:"******"
             *      LocalID:"15282968813090945"
             *      ToUserName:"******"
             *      Type:1
             *  }
             *  Scene:0
             * }*/
            var url  = "https://wx[number].qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg?pass_ticket={pass_ticket}";
            var temp = ReplaceKey(url);

            HttpWebRequest h = (HttpWebRequest)HttpWebRequest.Create(temp);

            h.AllowAutoRedirect = false;
            h.CookieContainer   = cookieContainer;
            h.Method            = "POST";
            h.Accept            = "application/json, text/plain, */*";
            h.ContentType       = "application/json;charset=utf-8";

            temp = p;
            temp = ReplaceHeaderKey(temp);

            var _msg = msg;

            if (robot)
            {
                _msg = GetMSG(msg, ToUserName);
            }

            temp = temp.Replace("{msg}", _msg);

            //对外输出信息
            ShowMsg(msg);

            temp = temp.Replace("{FromUserName}", ToUserName);
            temp = temp.Replace("{ToUserName}", FromUserName);
            temp = temp.Replace("{LocalID}", DateTimeToStamp(DateTime.Now) + "");
            Thread.Sleep(1);
            temp = temp.Replace("{ClientMsgId}", DateTimeToStamp(DateTime.Now) + "");

            byte[] bs = Encoding.UTF8.GetBytes(temp);
            using (Stream reqStream = h.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
                reqStream.Close();
            }
            try
            {
                HttpWebResponse r = (HttpWebResponse)h.GetResponse();

                //var list = GetAllCookies(cookieContainer);
                SendMsgResponse response = new SendMsgResponse()
                {
                    BaseResponse = new BaseResponse()
                };
                using (System.IO.StreamReader read = new System.IO.StreamReader(r.GetResponseStream()))
                {
                    string value = read.ReadToEnd();
                    response = value.ConvertJson <SendMsgResponse>();
                }
                r.Close();
                return(response);
            }
            catch (Exception ex)
            {
                return(new SendMsgResponse()
                {
                    BaseResponse = new BaseResponse()
                    {
                        ErrMsg = ex.Message
                    }
                });
            }
        }