第五讲 smart qq poll包处理 以及 私聊 群聊消息收发
发送 poll包
public static void Login_PostPoll()
{
try
{
string url = "http://d1.web2.qq.com/channel/poll2";
string dat = "{\"ptwebqq\":\"#{ptwebqq}\",\"clientid\":53999199,\"psessionid\":\"#{psessionid}\",\"key\":\"\"}";
dat = dat.Replace("#{ptwebqq}", Login_ptwebqq).Replace("#{psessionid}", Login_Psessionid);
dat = "r=" + HttpUtility.UrlEncode(dat);
HTTP.Post_Async_Action action = Message_Get;
HTTP.Post_Async(url, dat, action);
}
catch (Exception) { Login_PostPoll(); }
}
#endregion
#region 接收到消息的回调函数
/// <summary>
/// 接收到消息的回调函数
/// </summary>
/// <param name="data">接收到的数据(json)</param>
private static bool Running = true;
public static void Message_Get(string data)
{
Task.Run(() => Login_PostPoll());
if (Running)
Task.Run(() => Message_Process(data));//这个是处理收到信息的另外一个方法具体处理 在源码
}
以上就是poll包的处理以及 收到的信息处理
接下来是 发送的
上面是发送的连接以及 发送后返回的 poll包信息。代码如下
/// <summary>
///
/// </summary>
/// <param name="type">私聊0 群聊1 讨论组2</param>
/// <param name="uin">uin</param>
/// <param name="messageToSend">消息</param>
/// <param name="auto"></param>
/// <returns></returns>
public static bool Login_Postsend_buddy_msg(int type, string uin, string messageToSend)
{
if (messageToSend.Equals("") || uin.Equals(""))
return false;
string[] tmp = messageToSend.Split("{}".ToCharArray());
messageToSend = "";
for (int i = 0; i < tmp.Length; i++)
if (!tmp[i].Trim().StartsWith("..[face") || !tmp[i].Trim().EndsWith("].."))
messageToSend += "\\\"" + tmp[i] + "\\\",";
else
messageToSend += tmp[i].Replace("..[face", "[\\\"face\\\",").Replace("]..", "],");
messageToSend = messageToSend.Remove(messageToSend.LastIndexOf(','));
messageToSend = messageToSend.Replace("\r\n", "\n").Replace("\n\r", "\n").Replace("\r", "\n").Replace("\n", Environment.NewLine);
try
{
string to_groupuin_did, url;
switch (type)
{
case 0:
to_groupuin_did = "to";
url = "http://d1.web2.qq.com/channel/send_buddy_msg2";
break;
case 1:
to_groupuin_did = "group_uin";
url = "http://d1.web2.qq.com/channel/send_qun_msg2";
break;
case 2:
to_groupuin_did = "did";
url = "http://d1.web2.qq.com/channel/send_discu_msg2";
break;
default:
return false;
}
string postData = "{\"#{type}\":#{id},\"content\":\"[#{msg},[\\\"font\\\",{\\\"name\\\":\\\"繁体\\\",\\\"size\\\":10,\\\"style\\\":[0,0,0],\\\"color\\\":\\\"000000\\\"}]]\",\"face\":#{face},\"clientid\":53999199,\"msg_id\":#{msg_id},\"psessionid\":\"#{psessionid}\"}";
postData = "r=" + HttpUtility.UrlEncode(postData.Replace("#{type}", to_groupuin_did).Replace("#{id}", uin).Replace("#{msg}", messageToSend).Replace("#{face}", SelfInfo.face.ToString()).Replace("#{msg_id}", rand.Next(10000000, 99999999).ToString()).Replace("#{psessionid}", Login_Psessionid));
string dat = HTTP.Post(url, postData, "http://d1.web2.qq.com/proxy.html?v=20151105001&callback=1&id=2");
return dat.Equals("{\"errCode\":0,\"msg\":\"send ok\"}");
}
catch (Exception)
{
return false;
}
}
这一讲费了很多时间,中间又去查了资料。特别是处理返回的 包的json ,不知道怎么格式化,只能替换出来,拿到想要的数据。
到现在为止,整个新手smart qq http 协议的教程 已经可以说完成了,因为这个协议的功能非常单一,并且很多都已经被TX去掉的,就像获取好友的 真实QQ也已经是报错的。接下来就可以做图形界面,还有部分业务了(qq机器人自动回复等)