[c#] WebQQ群发限制的突破

 

    任何事物都有双面性。

      WebQQ作为一个跨平台的一站式QQ体验,继承了PC版QQ的很多优点,因此,也越来越受到营销行业人士的青睐。批量群发消息,省去很多重复性的工作。但是,众所周知的,由于群发所需的技术门槛极低,网上充斥着很大一部分的垃圾信息和骚扰广告,给很多QQ用户都带来了困扰,增加了其对群发消息的厌恶感。TX为了和谐聊天环境,采取了对WebQQ的诸多限制。本文试图从营销行业QQ群发的技术层面讨论一下,怎么样突破群发限制,并非鼓噪大家群发扰民!

     1.什么是WebQQ群发?

     WebQQ群发包括了对QQ好友和陌生批量性发送消息,以及QQ群群聊批量性发送消息到多个群。

     2.WebQQ有哪些限制?

     发送给好友的消息,发送端显示正常发送,好友却并没有收到;

     字数有限制,无法发送TX内定的屏蔽字词;

     发送图片数量以及不能重复发送的限制

     3. WebQQ群发的C#实现       

     本文假定您已经完成了WebQQ的二次登录,我们要实现的是对QQ好友信息的发送,以及多线程批量发送,QQ群消息发送,QQ群内成员消息发送。

       假设您已经获取了登录所带过来的Cookie,装在了一个全局变量QQGlobal.ACCountManager[this.Uin].CookieContainer里(其中QQGlobal是我设定的一个专门装载全局变量的公有类,AccountManager是一个字典Dictionary<string,Client>变量,用于盛放所有登录的QQ数据(格式为:uin,Client))为了能批量操作,我把每个登录的QQ都装载一个单独的类中,以免变量互相干扰,思维混乱。

     对QQ好友的信息发送

 

     下面一段代码中,uin是指的要发送的QQ号码,content是发送的消息正文,Msg_Id是我随机指定的一个int型变量,初值我设为50000(这个随便设置),没发送一次消息就加1,ClientId和PSessionId都是从登录过程中提取出来的参数,这些参数都可以在网上找到,如果没弄明白怎么来的,可以在后面关注我并留言。至于 HttpHelper.GetHtml(url)怎么来的,这个请看我前面一片文章“HttpWebRequest简单实用封装应用类”,里面详细讲到了封装的过程,也可以用大家自个封装好的。

 

View Code
 /// <summary>
        /// 发送消息给好友
        /// http://www.cnblogs.com/uu102/archive/2012/09/16/2687391.html 
        /// </summary>
        /// <param name="uin">好友QQ</param>
        /// <param name="content">消息内容</param>
        /// <returns></returns>
        public string send_buddy_msg2(string uin, string content)
        {
            string html = "";
            string url = "http://d.web2.qq.com/channel/send_buddy_msg2";   //发送消息
            this.Msg_Id++;
            if (string.IsNullOrEmpty(content)) content = " ";
            content = content.Replace("\n", "\\n");
            string r = string.Format("{{\"to\":{0},\"face\":{1},\"content\":\"[{2}[\\\"font\\\",{{\\\"name\\\":\\\"{3}\\\",\\\"size\\\":\\\"{4}\\\",\\\"style\\\":[0,0,0],\\\"color\\\":\\\"{5}\\\"}}]]\",\"msg_id\":{6},\"clientid\":{7},\"psessionid\":\"{8}\"}}",
                                     uin,
                                     QQGlobal.FreindsList[this.Uin].freindInfos[uin].Face,
                                     content,
                                     this.Font_Name,
                                     this.Font_Size,
                                     this.Color,
                                     this.Msg_Id,
                                     this.ClientId,
                                     this.PSessionId);
            string postString = string.Format("r={0}&clientid={1}&psessionid={2}", HttpUtility.UrlEncode(r, Encoding.UTF8), this.ClientId, this.PSessionId);
            html = HttpHelper.GetHtml(url, postString, QQGlobal.ACCountManager[this.Uin].CookieContainer);
            return html;
        }

 

上面的代码中一些莫名其妙冒出来的变量大家比划着理解就是了,因为这篇文章是我写好代码之后完成的,很多变量都牵扯到别的类中,所以可能会看起来一头雾水。
      QQ群的信息发送

 

      接下来,陌生人发送以及图片发送都先跳过去,单独作为一篇。来讲讲群发送。

     QQ群发送和QQ发送非常类似,因此只介绍一下变量代表的含义。ucode是从前面登录之后的一个请求提取出来的,具体请求页面以及怎样提取,暂时略过吧,以后有时间再说。代码就在下面列出。

View Code
    public string Send_qun_msg(string ucode, string content)
        {
            string url = "http://d.web2.qq.com/channel/send_qun_msg2";
            string r=string.Format("{{\"group_uin\":{0},\"content\":\"[\\\"{1}\\\",[\\\"font\\\",{{\\\"name\\\":\\\"\\\\u5b8b\\\\u4f53\\\",\\\"size\\\":\\\"10\\\",\\\"style\\\":[0,0,0],\\\"color\\\":\\\"000000\\\"}}]]\",\"msg_id\":{2},\"clientid\":\"{3}\",\"psessionid\":\"{4}\"}}",
                                     ucode,content,this.Msg_Id,this.ClientId,this.PSessionId);
            string postString = string.Format("clientid={0}&psessionid={1}&r={2}", this.ClientId, this.PSessionId,HttpUtility.UrlEncode(r, Encoding.UTF8));
             string html = HttpHelper.GetHtml(url, postString, QQGlobal.ACCountManager[this.Uin].CookieContainer);
            return html;
        }

     上述群消息的发送都省略图片发送这一段了,直接发到将来的一篇文章单独介绍。
     QQ群成员信息批量发送 

 QQ群成员首先要从群信息里提取出来,这是群成员发送的第一步。QQ群成员对应一个跟好友一样的临时号码,这个号码将用于发送等操作,而这些号码都要经过下面这一个Http请求才能返回提取。

View Code
 public Dictionary<string, string> GetGroupMemeber(string gcode)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            string html = "";
            string url = string.Format("http://s.web2.qq.com/api/get_group_info_ext2?gcode={0}&vfwebqq={1}&t=1341726562812", gcode, QQGlobal.ACCountManager[this.Uin].Vfwebqq);
            html = HttpHelper.GetHtml(url, QQGlobal.ACCountManager[this.Uin].CookieContainer);
            MatchCollection matches = new Regex(@"""nick"":""(?'nick'[^""]+)"",""province"":""(?'province'[^""]+)"",""gender"":""(?'gender'[^""]+)"",""uin"":(?'uin'[^,]+),""country"":""中国"",""city"":""(?'city'[^""]+)""", RegexOptions.IgnoreCase).Matches(html);
            for (int i = 0; i < matches.Count; i++)
            {
                dic.Add(matches[i].Groups["uin"].Value, matches[i].Groups["nick"].Value);
            }
            return dic;
        }

这些参数我想都不用解释了,纯粹的娘们活,细心。提交了这个请求之后,把返回的结果提取到一个自定义好的字典变量里,用于下一道工序——发送消息。这些代码说实在话,都是一些只可意会不可言传的干活!看着看着就明白了。所以还继续上代码了。

View Code
public string Send_sess_msg2(string to,string groupuin,string content)
        {
            string url = "",r="",html="",postString="";
            this.Msg_Id++;
            url = "http://d.web2.qq.com/channel/get_c2cmsg_sig2";
            string id = groupuin;
            postString = string.Format("id={0}&to_uin={1}&service_type=0&clientid={2}&psessionid={3}&t=1288591644319"
                                   ,id,to,ClientId,PSessionId );
                                      
           
            html = HttpHelper.GetHtml(url+"?"+postString, QQGlobal.ACCountManager[this.Uin].CookieContainer);
             url = "http://d.web2.qq.com/channel/send_sess_msg2";
            string group_sig=new Regex(@"value"":""(?'value'[^""]*)""").Match(html).Groups["value"].Value;
            
            if (string.IsNullOrEmpty(content)) content = " ";
            content = content.Replace("\n", "\\n");
            r = string.Format("{{\"to\":{0},\"group_sig\":\"{1}\",\"face\":{2},\"content\":\"[\\\"{3}\\\",[\\\"font\\\",{{\\\"name\\\":\\\"\\\\u5b8b\\\\u4f53\\\",\\\"size\\\":\\\"10\\\",\\\"style\\\":[0,0,0],\\\"color\\\":\\\"000000\\\"}}]]\",\"msg_id\":{4},\"service_type\":0,\"clientid\":\"{5}\",\"psessionid\":\"{6}\"}}",
                                to,
                                group_sig,
                                528,
                                content,
                                Msg_Id,
                                ClientId,
                                PSessionId
                                );
             postString = string.Format("r={0}&clientid={1}&psessionid={2}", HttpUtility.UrlEncode(r, Encoding.UTF8), this.ClientId, this.PSessionId);
            html = HttpHelper.GetHtml(url, postString, QQGlobal.ACCountManager[this.Uin].CookieContainer);
            return html;
        }

到这里为止,能做的都做了(忘了说到讨论组的创建和群发了,有机会再补上)。对这些东西感兴趣的朋友,请保持对本博客的关注,有了大家的交流,才会对这一行业看得更透彻。

文章未完,后篇待续!每天都有更新,请朋友们继续关注!

posted on 2012-09-16 19:44  凌晨的搜索者  阅读(10517)  评论(14编辑  收藏  举报

导航