本篇主要是: 獲取好友列表,群列表
我會盡量詳細一點,盡我所知的分享一些可能大家已經掌握的或者還不清楚的經驗
利於大家閱讀,文章樣式不再復雜化,根據內容取固定色
全系列預計會有這些步驟,當然某些步驟可能會合並:
- 驗證碼
- 第一次登陸
- 第二次登陸
- 保持在線和接收消息
- 獲取好友和群列表
- 發送消息
- 變成智能的(*゚∀゚*)
獲取好友 1-獲取QQ好友的hash算法
P = function (b, j) { for (var a = j + "password error", i = "", E = []; ;) if (i.length <= a.length) { if (i += b, i.length == a.length) break; } else { i = i.slice(0, a.length); break } for (var c = 0; c < i.length; c++) E[c] = i.charCodeAt(c) ^ a.charCodeAt(c); a = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"]; i = ""; for (c = 0; c < E.length; c++) i += a[E[c] >> 4 & 15], i += a[E[c] & 15]; return i }
傳入了2個參數:QQ號碼
、ptwebqq(文章2中從cookie中拿到)
獲取好友 2-請求
- 地址:
http://s.web2.qq.com/api/get_user_friends2
- referer:
http://s.web2.qq.com/proxy.html?v=20130916001&callback=1&id=1
- post字符串:
string.Format("r={{\"vfwebqq\":\"{0}\",\"hash\":\"{1}\"}}", this.VfWebQQ, this.Hash);
獲取好友 3-返回和分析
返回json對象由兩部分組成:retcode
、result
,前者表示請求是否成功不提,我們主要看result,里面包含了這些東西:
- categories,分類,即你的QQ好友分組,
index
可以看做分組表的主鍵,sort
排序,name
該組的別名。《我的好友》分組默認是index:0 - friends好友,
flag
表示默認頭像序號,后文同不提,uin
是貫穿全文的參數,是在網頁關閉前,瀏覽器客戶端的唯一標識,categories
對應上面分組信息 - info 儲存了好友的信息--昵稱
nick
,uin
對應唯一標識 - marknames 備注名稱,同樣
uin
對應唯一標識,markname
對應備注名稱,這里要說的是,如果沒有備注,在這里是不顯示的 - vipinfo,vip信息,無用
我建立了一個類去用於反序列化它
class JsonFriendModel { public int retcode { get; set; } public paramResult result = new paramResult(); public class paramResult { /// /// 分組信息 /// public List categories = new List(); /// /// 好友匯總 /// public List friends = new List(); /// /// 好友信息 /// public List info = new List(); /// /// 備注 /// public List marknames = new List(); /// /// 分組 /// public class paramCategories { public string index { get; set; } public int sort { get; set; } public string name { get; set; } } /// /// 好友匯總 /// public class paramFriends { public string flag { get; set; } public string uin { get; set; } public string categories { get; set; } } /// /// 好友信息 /// public class paramInfo { public string face { get; set; } public string nick { get; set; } public string uin { get; set; } } /// /// 備注 /// public class paramMarkNames { public string uin { get; set; } public string markname { get; set; } } } }
小擴展 linq中的left join 左查詢
上面返回的result信息,包含了4個對象,互相使用uin或者其它進行關聯,使用for循環固然可以,當然有更漂亮的方法也會跟大家分享一下,如果寫的不好也請大家多提意見:
var query = from f in model.result.friends join i in model.result.info on f.uin equals i.uin into table1 from t1 in table1.DefaultIfEmpty() join m in model.result.marknames on f.uin equals m.uin into table2 from t2 in table2.DefaultIfEmpty() select new Friend() { Uin = f.uin, Face = t1 == null ? string.Empty : t1.face, Category = f.categories, Nick = t1 == null ? string.Empty : t1.nick, MarkName = t2 == null ? string.Empty : t2.markname };
以上是使用了left join 多表進行關聯查詢,model即對應了返回json的result屬性
獲取群 1-請求信息
- 地址:
http://s.web2.qq.com/api/get_group_name_list_mask2
- referer:
http://s.web2.qq.com/proxy.html?v=20130916001&callback=1&id=1
- post字符串:
string.Format("r={{\"vfwebqq\":\"{0}\",\"hash\":\"{1}\"}}", this.VfWebQQ, this.Hash);
獲取群 2-返回信息
結構和獲取好友有些類似,不同的是:群的唯一標識是gid,好友的是uin,名稱不同。以下是對應的實體類:
class JsonGroupModel { public int retcode { get; set; } public paramResult result = new paramResult(); public class paramResult { public List gnamelist = new List(); public class paramGnamelist { public string flag { get; set; } public string gid { get; set; } public string code { get; set; } public string name { get; set; } } } }
到目前為止,已經可以完整的登陸,並保持在線,獲取消息(解析消息還未說明),獲取好友和群列表
接下來我想對每一篇文章寫上對應的demo,來幫助大家更好的理解整個過程,畢竟文章主要講的是流程,實際操作中可能遇到這種或者那種的問題。
為之前的文章附上demo,可能會花一點時間,原計划的每日一更就難實現了...盡量吧
使用C#模擬http請求可以參考猛戳這里
您有沒有對這篇文章感興趣呢?
自制QQ分組控件(自己寫的,湊合用=。=):
一步一步來做WebQQ機器人-(四)(獲取好友列表和群列表):
一步一步來做WebQQ機器人-(四)(獲取好友列表和群列表):
本步驟的demo,一步一步來做WebQQ機器人-(四)(獲取好友列表和群列表+自制QQ分組控件),更新於2015/2/5
可以到該系列最后一篇文章查看是否可能有最新demo
轉載請保留本頁鏈接:http://www.cnblogs.com/lianmin/p/4237723.html
.