C# WebQQ協(xié)議群發(fā)機器人(二)
|
admin
2016年12月23日 18:56
本文熱度 6216
|
本文出處http://blog.csdn.net/zhujunxxxxx/ 如需轉(zhuǎn)載請注明出處! 接著上一篇http://blog.csdn.net/zhujunxxxxx/article/details/38931287我講的內(nèi)容 如果有人需要源碼可以聯(lián)系我 qq: 10588690,但是不是無償提供,請大家理解。
上面實現(xiàn)的都是與群操作相關(guān)的 接下來是獲取好友信息的方法 -
-
-
- public void GetFriendList()
- {
- Dictionary<string, Object> r = new Dictionary<string, Object>();
- r.Add("h", "hello");
- r.Add("hash", this.hash);
- r.Add("vfwebqq", this.vfwebqq);
- Dictionary<string, Object> data = new Dictionary<string, Object>();
- data.Add("r", JsonConvert.SerializeObject(r));
- string postdata = Tool.MakePostData(data);
-
- HttpItem item = new HttpItem()
- {
- URL = "http://s.web2.qq.com/api/get_user_friends2",
- Method = "POST",
- IsToLower = false,
- Timeout = 100000,
- Postdata = postdata,
- Host = HOST[1],
- Referer = REFERER[1],
- UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36",
- ContentType = "application/x-www-form-urlencoded",
- ResultType = ResultType.String
- };
- HttpResult result = http.GetHtml(item);
- JObject ret = (JObject)JsonConvert.DeserializeObject(result.Html);
- JObject retjson = (JObject)ret["result"];
-
- foreach (JToken jk in retjson["marknames"])
- {
- this.friendlist.Add(jk["uin"].ToString(), jk["markname"].ToString());
- }
-
- foreach (JToken jk in retjson["info"])
- {
- if (!this.friendlist.ContainsKey(jk["uin"].ToString()))
- this.friendlist.Add(jk["uin"].ToString(), jk["nick"].ToString());
- }
- }
這個方法可以獲取到好友的信息,分為兩部分一部分是有備注名的好友信息,一部分是無備注名的好友信息
把這部分信息保存下來以后有用 獲取一個具體好友的信息(自己的信息也是通過這個獲?。?/p> - public void GetFriendInfo(string tuin)
- {
- string url = "http://s.web2.qq.com/api/get_friend_info2?tuin={$tuin}&verifysession=&code=&vfwebqq={$vfwebqq}&t=1402534798024";
- url = url.Replace("{$vfwebqq}", vfwebqq);
- url = url.Replace("{$tuin}", tuin);
-
- HttpItem item = new HttpItem()
- {
- URL = url,
- Encoding = System.Text.Encoding.GetEncoding("utf-8"),
- Method = "get",
- IsToLower = false,
- Timeout = 100000,
- ReadWriteTimeout = 30000,
- Host = HOST[1],
- Referer = REFERER[1],
- UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",
- ContentType = "application/x-www-form-urlencoded",
- ResultType = ResultType.String,
- };
- HttpResult result = http.GetHtml(item);
- }
接下來是向某一個好友發(fā)送信息
- public void SendPersonMsg(string tuin,string msg)
- {
- string style = "\"{content}\"";
- string temp = "";
- temp += style.Replace("{content}", msg) + ",";
- temp = temp.Substring(0, temp.Length - 1);
- Random rd = new Random();
- int msg_id = (rd.Next(100000) + 100000);
- string content = "[{$msg},\"\",[\"font\",{\"name\":\"宋體\",\"size\":\"10\",\"style\":[0,0,0],\"color\":\"000000\"}]]";
- content = content.Replace("{$msg}", temp);
-
-
- Dictionary<string, Object> r = new Dictionary<string, Object>();
- r.Add("to", tuin);
- r.Add("face", 561);
- r.Add("content", content);
- r.Add("msg_id",msg_id);
- r.Add("clientid", this.clientid);
- r.Add("psessionid", this.psessionid);
- Dictionary<string, Object> data = new Dictionary<string, Object>();
- data.Add("r", JsonConvert.SerializeObject(r));
- data.Add("clientid", this.clientid);
- data.Add("psessionid", this.psessionid);
- string postdata = Tool.MakePostData(data);
-
- HttpItem item = new HttpItem()
- {
- URL = "http://d.web2.qq.com/channel/send_buddy_msg2",
- Accept = "*/*",
- Method = "POST",
- IsToLower = false,
- KeepAlive = true,
- Timeout = 100000,
- Postdata = postdata,
- Host = HOST[0],
- Referer = REFERER[0],
- UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2041.4 Safari/537.36",
- ContentType = "application/x-www-form-urlencoded",
- ResultType = ResultType.String
- };
-
- HttpResult result = http.GetHtml(item);
- JObject ret = (JObject)JsonConvert.DeserializeObject(result.Html);
- }
好了,這篇和上一篇文章就基本上實現(xiàn)了WebQQ的基本功能, 為了保持QQ一直在線我們比如一定時間向服務(wù)器發(fā)送心跳請求,心跳請求的返回值就是我們收到的消息,這個是很重要的所以想對群消息進行處理的話都需要在這里處理它 - public void HeartBreak()
- {
-
-
-
-
-
-
-
-
-
-
-
-
- string postdata = "r=%7B%22clientid%22%3A%22{$clientid}%22%2C%22psessionid%22%3A%22{$psessionid}%22%2C%22key%22%3A0%2C%22ids%22%3A%5B%5D%7D&clientid={$clientid}&psessionid={$psessionid}";
- postdata=postdata.Replace("{$clientid}",this.clientid);
- postdata = postdata.Replace("{$psessionid}", this.psessionid);
- HttpItem item = new HttpItem()
- {
- URL = "http://d.web2.qq.com/channel/poll2",
- Method = "POST",
- IsToLower = false,
- Timeout = 100000,
- Postdata = postdata,
- Host = HOST[0],
- Referer = REFERER[0],
- UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36",
- ContentType = "application/x-www-form-urlencoded",
- ResultType = ResultType.String
- };
- HttpResult result = http.GetHtml(item);
- JObject ret = (JObject)JsonConvert.DeserializeObject(result.Html);
- JObject retjson = (JObject)ret["result"][0];
- if (retjson["poll_type"].ToString() == "group_message")
- {
- JObject msg = (JObject)retjson["value"];
- ProcessMsg(msg);
- }
- Thread.Sleep(2000);
-
- }
在發(fā)送信息的上面還有一些可以拓展的,比如圖片等,我這一部分還沒有研究,如果有誰做過的話希望分享一下咯
該文章在 2016/12/23 18:56:47 編輯過
|
|