任何事物都有雙面性。
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簡單實用封裝應用類”,里面詳細講到了封裝的過程,也可以用大家自個封裝好的。

/// <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是從前面登錄之后的一個請求提取出來的,具體請求頁面以及怎樣提取,暫時略過吧,以后有時間再說。代碼就在下面列出。

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請求才能返回提取。

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; }
這些參數我想都不用解釋了,純粹的娘們活,細心。提交了這個請求之后,把返回的結果提取到一個自定義好的字典變量里,用於下一道工序——發送消息。這些代碼說實在話,都是一些只可意會不可言傳的干活!看着看着就明白了。所以還繼續上代碼了。

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; }
到這里為止,能做的都做了(忘了說到討論組的創建和群發了,有機會再補上)。對這些東西感興趣的朋友,請保持對本博客的關注,有了大家的交流,才會對這一行業看得更透徹。
文章未完,后篇待續!每天都有更新,請朋友們繼續關注!