博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 获取QQ群数据的实现
阅读量:6838 次
发布时间:2019-06-26

本文共 10004 字,大约阅读时间需要 33 分钟。

一,分析

1,群数据获取

        当访问我们通过Fiddler可以查看到QQ群列表是从这个URL获取到的群列表信息

          

   其中返回的json数据,json构成如下

             

          从上面我们可以看出c包含的是群列表信息,r包含的是服务器的信息,其中c的某一项构成如下图所示

                      

        从上面可以看出项名称就是群号,owner为所有者,name为群名称,memo群描述,brief为群公告信息,还有其他的一些信息,大家自己去匹配,我这里就不一一解释了.

   2, 单个群的成员获取,在上面我们分析了如何获取群列表,下面我们将分析如何取得单个群成员的数据 

      当我们点开某个群可以看到有查看通讯录的选项 
      
     此时我们通过Fiddler监视可以发现,群成员信息,通过如下地址取得 
            
    但是我经过几次点击测试,发现URL参数规则如下 
       
      贴出参数构造规则 
     

const string groupMemberUrl = "http://qun.qq.com/air/{0}/addr/index/type/1/p/{1}?w=n&_={2}";
string memberURL = string.Format(groupMemberUrl, gInfo.Number, page, JavascriptAction.Random());
我们按照这样的地址去请求,就会得到QQ群成员的HTML,然后我在解析HTML就可以得到相应的数据了
 

二,实现 


    1, 获取群列表

public List
GetGroupInfos()
{
if (!isLogin) Login();
List
giList = new List
();
string json = GetWebData
(string.Format(groupListUrl, JavascriptAction.Random()));
try
{
var jsonObj = JavaScriptEngine.Run("var m=" + json + ";m.c") as Dictionary
;
foreach (var key in jsonObj.Keys)
{
var gi = (jsonObj[key] as Dictionary
);
if (!gi.ContainsKey("owner")) continue;
string owner = gi["owner"].ToString();
string name = gi["name"].ToString();
string max_member = gi["max_member"].ToString();
string create_time = gi["create_time"].ToString();
string notice = gi["brief"].ToString();
string number = key;
string memo = gi["memo"].ToString();
giList.Add(new GroupInfo()
{
CreateTime = JavascriptAction.GetTimeByJsTime(Convert.ToInt64(create_time)),
MaxNumber = Convert.ToInt32(max_member),
Memo = memo,
Name = name,
Notice = notice,
Number = number,
Owner = owner
});
}
}
catch { }
return giList;
}

         上面的函数实现了群列表的获取,需要说明的是获取到的json是同过javascript引擎来运行的,javascript运行我通过Javascript .NET()来实现的,获取web数据参考我的blog的另外的文章,文章有实现源代码,至于登陆我最后会说明

   2, 获取群成员 

      

public List
GetMemberInfo(GroupInfo gInfo)
{
if (!isLogin) Login();
List
gmis = new List
();
int page = 1;
int pageCount = 0;
GETMEMBER:
string memberURL = string.Format(groupMemberUrl, gInfo.Number, page, JavascriptAction.Random());
string html = GetWebData
(memberURL);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
if (pageCount < 1)
{
var seeBtn = doc.DocumentNode.SelectSingleNode(pageBtnPath);
pageCount = Convert.ToInt32(seeBtn.Attributes["allpage"].Value);
}
var trs = doc.DocumentNode.SelectNodes(memberTrPath);
foreach (var tr in trs)
{
var tds = tr.Descendants("td").ToList();
if (tds.Count < 1) continue;
string groupNick = tds[1].Element("a").InnerText.Trim();
string qq = tds[2].Element("span").InnerText.Trim();
gmis.Add(new QQGroupMemberInfo()
{
NickName = groupNick,
QQ = qq
});
}
if (page < pageCount)
{
page++;
goto GETMEMBER;
}
return gmis;
}

   上面的函数实现了群成员的获取,其中HTML的解析式通过HtmlAgilityPack()来实现,HtmlAgilityPack通过XPATH语法去解析实现的,关于xpath不是很难,大家查查资料就会了 

   3,关于登陆的实现 
     QQweb端的登陆有两种实现,第一种是web登陆,通过这样的URL实现来实现登陆,具体的实现原理,我前面有文章描述过了,这里就不罗嗦了,把类贴出来供大家参考

public class LoginQQ : WebPageAction, ILogin, IValidateCodeAction
{
static JavaScriptEngine _javaEngine = new JavaScriptEngine();
static bool _isInitJava = false;
Random _r = new Random();
const string loginFormUrl = "http://ui.ptlogin2.qq.com/cgi-bin/login?link_target=blank&appid=15000101&hide_title_bar=1&s_url=http%3A%2F%2Fim.qq.com%2Floginproxy.html%3Flogin_level%3D2&f_url=loginerroralert&target=self";
private User User { get; set; }
string _qq = "";
public LoginQQ(CookieManager cookieManager, User user)
: base(user.UserName)
{
_cookieManager = cookieManager;
User = user;
_qq = user.UserName.Split('@')[0];
if (!_isInitJava)
{
_javaEngine.Run(System.IO.File.ReadAllText(@"JavaScript\qq\md5_3.js"));
_isInitJava = true;
}
}
 
public override JavaScriptEngine JavaScriptEngine
{
get
{
return _javaEngine;
}
set
{
_javaEngine = value;
}
}
 
string vc_type = "";
public bool Login(string referer)
{
DateTime beginTime = DateTime.Now;
string html = "";
html = GetWebData
(loginFormUrl);
if (!regForm.IsMatch(html))
ThrowException(507, html);
var form = GetFormDataByFormHtml(regForm.Match(html).Value);
string url = "http://ptlogin2.qq.com/check?uin=" + _qq + "&appid=15000101&" + JavascriptAction.Random();
html = GetWebData
(url, referer: loginFormUrl);
string vcode = "";
if (html.Contains("ptui_checkVC('1','"))
{
vc_type = html.Replace("ptui_checkVC('1','", "").Replace("'", "").Replace(")", "").Replace(";", "");
form = new NameValueCollection();
if (!ValidationImageCode(form, loginFormUrl)) { return false; };
vcode = form["verifycode"];
}
else if (html.Contains("ptui_checkVC('0','"))
{
vcode = html.Replace("ptui_checkVC('0','", "").Replace("'", "").Replace(")", "").Replace(";", "");
}
TimeSpan _tspan = TimeSpan.FromTicks(DateTime.Now.Ticks - beginTime.Ticks);
if (_tspan.Seconds < 20)
Thread.Sleep(TimeSpan.FromSeconds(_r.Next(20 - _tspan.Seconds, 30)));
string pMd5 = GetPwdString(User.Pwd, vcode);
string action = "0-0" + "-" + (JavascriptAction.GetTime(DateTime.Now) - JavascriptAction.GetTime(beginTime));
url = "http://ptlogin2.qq.com/login?u=" + _qq + "&p=" + pMd5 + "&verifycode=" + vcode + "&aid=15000101&u1=http%3A%2F%2Fim.qq.com%2Floginproxy.html%3Flogin_level%3D2&h=1&ptredirect=0&ptlang=2052&from_ui=1&dumy=&fp=loginerroralert&action=" + action + "&mibao_css=";
html = GetWebData
(url, referer: loginFormUrl);
if (html.Contains("登录成功"))
{
return true;
}
return false;
}
 
private string GetPwdString(string pwd, string vcode)
{
string script = "md5(md5_3('" + pwd + "') + '" + vcode + "')";
return JavaScriptEngine.Run(script).ToString();
}
 
public bool ValidateLogin(string referer)
{
var cks = _cookieManager.GetCookies(new Uri(referer).Host);
if (cks["uin"] != null && cks["skey"] != null)
{
return true;
}
return false;
}
 
public bool ValidationCode(string code, string referer)
{
return true;
}
 
public Image GetValidateCode(string referer)
{
string url = "http://captcha.qq.com/getimage?aid=15000101&r=" + JavascriptAction.Random() + "&uin=" + _qq + "&vc_type=" + vc_type;
return GetWebData(url, referer: referer);
}
 
public bool ValidationImageCode(NameValueCollection customFormItems, string referer)
{
ValidateReturn code = null;
do
{
Image image = GetValidateCode(referer);
code = this.FireValidateCode(image);
if (code.IsCancel)
{
return false;
}
if (customFormItems.AllKeys.Contains("verifycode"))
{
customFormItems.Remove("verifycode");
}
customFormItems.Add("verifycode", code.Code);
}
while (code.IsChange || !ValidationCode(code.Code, referer));
return true;
}
 
 
}

第二种就是从客户端点击,如QQ空间按钮,客户端会调用浏览器跳转

这样的地址,通过这个URL实现登陆,那么我们只需要HOOK ShellExecuteExW这个入口点就可以实现URL的截取,关于钩子如何实现,我前面有文章已说过,同样贴上代码供大家参考 

public class ClientQQWebction : WebPageAction, IDisposable
{
const string loginUrl = "http://ptlogin2.qq.com/jump?ptlang={2}&clientuin={0}&clientkey={1}&u1={3}";
const string getEmail = "http://accountadm.qq.com/cgi-bin/account/ajaxgetmail?uin={0}";
const string groupListUrl = "http://qun.qq.com/air/group/mine?w=a&_={0}";
const string groupMemberUrl = "http://qun.qq.com/air/{0}/addr/index/type/1/p/{1}?w=n&_={2}";
const string pageBtnPath = "//input[@type=\"button\" and @value=\"查看\" and @act=\"gotopage\"]";
const string memberTrPath = "//table[@class=\"addressList\"]/tr";
 
CleintKeyInfo _cleintKeyInfo = null;
bool isLogin = false;
public ClientQQWebction(CleintKeyInfo cki)
{
_cleintKeyInfo = cki;
}
 
private void Login()
{
string loginURL = string.Format(loginUrl, _cleintKeyInfo.ClientUin, _cleintKeyInfo.ClientKey, _cleintKeyInfo.PtLang, string.Format(getEmail, _cleintKeyInfo.ClientUin));
var loca = GetWebData
(loginURL);
if (_cookieManager.GetCookies("qq.com")["skey"] != null)
{
isLogin = true;
return;
}
throw new Exception("登陆失败");
}
 
public string GetEmail()
{
if (!isLogin) Login();
string emailUrl = String.Format(getEmail, _cleintKeyInfo.ClientUin);
var html = GetWebData
(emailUrl);
if (regEmail.IsMatch(html))
return regEmail.Match(html).ToString();
else
return null;
}
 
public List
GetGroupInfos()
{
if (!isLogin) Login();
List
giList = new List
();
string json = GetWebData
(string.Format(groupListUrl, JavascriptAction.Random()));
try
{
var jsonObj = JavaScriptEngine.Run("var m=" + json + ";m.c") as Dictionary
;
foreach (var key in jsonObj.Keys)
{
var gi = (jsonObj[key] as Dictionary
);
if (!gi.ContainsKey("owner")) continue;
string owner = gi["owner"].ToString();
string name = gi["name"].ToString();
string max_member = gi["max_member"].ToString();
string create_time = gi["create_time"].ToString();
string notice = gi["brief"].ToString();
string number = key;
string memo = gi["memo"].ToString();
giList.Add(new GroupInfo()
{
CreateTime = JavascriptAction.GetTimeByJsTime(Convert.ToInt64(create_time)),
MaxNumber = Convert.ToInt32(max_member),
Memo = memo,
Name = name,
Notice = notice,
Number = number,
Owner = owner
});
}
}
catch { }
return giList;
}
 
 
public List
GetMemberInfo(GroupInfo gInfo)
{
if (!isLogin) Login();
List
gmis = new List
();
int page = 1;
int pageCount = 0;
GETMEMBER:
string memberURL = string.Format(groupMemberUrl, gInfo.Number, page, JavascriptAction.Random());
string html = GetWebData
(memberURL);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
if (pageCount < 1)
{
var seeBtn = doc.DocumentNode.SelectSingleNode(pageBtnPath);
pageCount = Convert.ToInt32(seeBtn.Attributes["allpage"].Value);
}
var trs = doc.DocumentNode.SelectNodes(memberTrPath);
foreach (var tr in trs)
{
var tds = tr.Descendants("td").ToList();
if (tds.Count < 1) continue;
string groupNick = tds[1].Element("a").InnerText.Trim();
string qq = tds[2].Element("span").InnerText.Trim();
gmis.Add(new QQGroupMemberInfo()
{
NickName = groupNick,
QQ = qq
});
}
if (page < pageCount)
{
page++;
goto GETMEMBER;
}
return gmis;
}
 
 
public void Dispose()
{
_cookieManager = null;
 
JavaScriptEngine = null;
 
}
}

OK,到此完成,下篇实现qq好友数据获取

最近失业了,求职中……… 

转载地址:http://xdkul.baihongyu.com/

你可能感兴趣的文章
easyui easyui-filebox 显示中文
查看>>
FG面经Prepare: BST to Double LinkedList
查看>>
360开源的pika
查看>>
POJ 2503-Babelfish(map)
查看>>
提高sqlmap爆破效率
查看>>
HDOJ 5288 OO’s Sequence 水
查看>>
centos 扩容
查看>>
采用模拟账号读取Exchange server未读邮件的注意事项(链接邮箱问题)【转】
查看>>
38..Node.js工具模块---底层的网络通信--Net模块
查看>>
小tip: DOM appendHTML实现及insertAdjacentHTML
查看>>
图文介绍openLDAP在windows上的安装配置
查看>>
天气接口
查看>>
“C语言” 读书札记(二)之[程序和编程语言]
查看>>
C# winfrom设置循环暂停和继续 原文转自:http://blog.csdn.net/qwldcl/article/details/3970784...
查看>>
架构师给程序员的一封信
查看>>
[openstack问题]dashboard无法登陆进去的问题
查看>>
tar and war的一些命令
查看>>
http://poj.org/problem?id=2253
查看>>
【译】用Fragment解决屏幕旋转(状态发生变化)状态不能保持的问题
查看>>
SpringMVC 配置log4j
查看>>