欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.net实现扫描二维码登录webqq群抓取qq群信息

程序员文章站 2023-04-06 20:53:51
一.流程 1. //获得二维码的qrsig,cookie标志 2. //登录二维码获得二维码的状态,及最新的url 3. //登录此网址,获得Cookies 4.//cookies,筛选出skey信息,获得post的参数 5. 提交POST 二. 代码 /// /// 获得QQ群 ......

一.流程

1. //获得二维码的qrsig,cookie标志

2. //登录二维码获得二维码的状态,及最新的url

3. //登录此网址,获得cookies

4.//cookies,筛选出skey信息,获得post的参数

5. 提交post

二. 代码

/// <summary>
/// 获得qq群返回的信息
/// </summary>
/// <returns></returns>
public string getqqgroupmessage()
{
try
{
random random = new random();
var da = random.nextdouble();
string qrcodeurl = @"https://ssl.ptlogin2.qq.com/ptqrshow?appid=715030901&e=2&l=m&s=3&d=72&v=4&t=" + da.tostring() + @"&daid=73&pt_3rd_aid=0";
string qqgroupcheckurl = null;
string qqgroupnumberurl = @"http://qun.qq.com/cgi-bin/qun_mgr/search_group_members";
string qrsig = null;
string setcookies = null;
string postdata = null;
string responsemessage = null;
httpclient httpclient = new httpclient();

qrsig = httpclient.getqrcode(qrcodeurl); //获得二维码的qrsig,cookie标志

qqgroupcheckurl = login_qrstatu(httpclient, qrsig); //登录二维码获得二维码的状态,及最新的url

httpclient.get(out setcookies, qqgroupcheckurl); //登录此网址,获得cookies

postdata = getpostdata(setcookies); //cookies,筛选出skey信息,获得post的参数

responsemessage = httpclient.post(qqgroupnumberurl, postdata);
loghelp.logdebug("get qq group message success!");
return responsemessage;
}
catch (exception ex)
{
loghelp.logdebug("get qq group message failed!" + ex);
return null;
}
}

/// <summary>
/// 扫描二维码,并获得最新的url
/// </summary>
/// <param name="httpclient">
/// httpclient
/// </param>
/// <param name="qrsig">
/// qrsig
/// </param>
/// <returns></returns>
private string login_qrstatu(httpclient httpclient,string qrsig)
{
int ptqrtoken = httpclient.hashconvert(qrsig);
string url = @"https://ssl.ptlogin2.qq.com/ptqrlogin?u1=https%3a%2f%2fqun.qq.com%2f&ptqrtoken=" +
ptqrtoken.tostring() +
@"&ptredirect=1&h=1&t=1&g=1&from_ui=1&ptlang=2052&action=0-1-1520572499521&js_ver=10233&js_type=1&login_sig=v-enlbkgzcaodxlgch2k8di6krubbwgwzfpao*kfmwvybewgcqluqvlj4yaneo61&pt_uistyle=40&aid=715030901&daid=73&";
string referer = @"https://xui.ptlogin2.qq.com/cgi-bin/xlogin?appid=715030901&daid=73&pt_no_auth=1&s_url=https%3a%2f%2fqun.qq.com%2f";
string strlogionurl = null;
string setcookies = null;
int timecount = 0;
while (strlogionurl == null)
{
timecount++;
thread.sleep(1000);
string dat = httpclient.get(out setcookies, url, referer);
string[] temp = dat.split('\'');
switch (temp[1])
{

case ("65"):
loghelp.logdebug("当前登录状态:二维码失效,请稍后");
break;
case ("66"):
loghelp.logdebug("当前登录状态:二维码有效,请扫描");
break;
case ("67"):
loghelp.logdebug("当前登录状态:二维码已扫描,请确认");
break;
case ("0"):
loghelp.logdebug("当前登录状态:确认成功,请稍候");
strlogionurl = temp[5];
break;
default:
loghelp.logdebug("登录信息有误,重新扫描!");
break;
}
if(timecount>300) //5min内有效
{
break;
}
}
return strlogionurl;
}

/// <summary>
/// post方法的参数
/// </summary>
/// <param name="setcookie">setcookies</param>
/// <returns></returns>
private string getpostdata(string setcookie)
{
try
{
string skey = httpclient.getskey(setcookie);
//string postdata = "gc=621859311&st=0&end=800&sort=0&bkn=" + httpclient.getbkn(skey).tostring();
string postdata = keywordslibconfigmanager.keywordslibconfig.postdata + httpclient.getbkn(skey).tostring();
return postdata;
}
catch (exception ex)
{
loghelp.logerror("get postdata error!" + ex);
return null;
}
}

三. html解析,post和get方法

namespace test.htmlparse
{
/// <summary>
/// //网络通信相关
/// </summary>
public class httpclient
{


public static cookiecontainer cookies = new cookiecontainer();
static cookiecollection cookiecollection = new cookiecollection();
static cookiecontainer cookiecontainer = new cookiecontainer();

/// <summary>
/// 获得response内容
/// </summary>
/// <param name="setcookies">out cookie内容</param>
/// <param name="referer"></param>
/// <param name="timeout"></param>
/// <param name="encode"></param>
/// <param name="noproxy"></param>
/// <returns></returns>
public string get(out string setcookies,
string url,
string referer = "https://xui.ptlogin2.qq.com/cgi-bin/xlogin?appid=715030901&daid=73&pt_no_auth=1&s_url=https%3a%2f%2fqun.qq.com%2f",
int timeout = 100000,
encoding encode = null,
bool noproxy = false)
{
if (url == null)
{
setcookies = null;
return null;
}

string dat;
httpwebresponse res = null;
httpwebrequest req;
try
{
req = (httpwebrequest)webrequest.create(url);
req.cookiecontainer = cookies;
req.allowautoredirect = false;
req.timeout = timeout;
req.referer = referer;
if (noproxy)
req.proxy = null;
req.useragent = "mozilla/5.0 (windows nt 6.1; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/65.0.3325.146 safari/537.36";
res = (httpwebresponse)req.getresponse();
setcookies = res.headers.get("set-cookie");
cookies.add(res.cookies);
}
catch (exception ex)
{
loghelp.logerror(string.format("get {0} response error! ex:{1}", url, ex));
setcookies = null;
return null;
}
streamreader reader;
reader = new streamreader(res.getresponsestream(), encode == null ? encoding.utf8 : encode);
dat = reader.readtoend();
res.close();
req.abort();
loghelp.logdebug(string.format("get method request succeed! - setcookies={0},url ={1}", setcookies, url));
return dat;
}

/// <summary>
/// qr二维码,返回qrsig值
/// </summary>
/// <param name="url"></param>
/// <param name="referer"></param>
/// <param name="timeout"></param>
/// <param name="encode"></param>
/// <param name="noproxy"></param>
public string getqrcode(
string url,
string referer = "https://xui.ptlogin2.qq.com/cgi-bin/xlogin?appid=715030901&daid=73&pt_no_auth=1&s_url=https%3a%2f%2fqun.qq.com%2f",
int timeout = 100000,
encoding encode = null,
bool noproxy = false)
{
if (url == null)
return null;

string qrsigcookie = null;
httpwebresponse res = null;
httpwebrequest req;
try
{
req = (httpwebrequest)webrequest.create(url);
req.cookiecontainer = cookies;
req.allowautoredirect = false;
req.timeout = timeout;
req.referer = referer;
if (noproxy)
req.proxy = null;
req.useragent = "mozilla/5.0 (windows nt 6.1; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/65.0.3325.146 safari/537.36";
res = (httpwebresponse)req.getresponse();
cookies.add(res.cookies);
}
catch (exception ex)
{
loghelp.logerror(string.format("getqr {0} response error! ex:{1} ", url, ex));
throw ex;
}
try
{
streamreader reader;
reader = new streamreader(res.getresponsestream(), encode == null ? encoding.utf8 : encode);
bitmap bmap = new bitmap(res.getresponsestream());
bmap.save(@"d:\qrcode.jpg");
//发送邮件
string title = "qq群登录二维码";
string content = "请在五分钟内扫描登录二维码,过期失效!";
string[] receiveaddrs = keywordslibconfigmanager.keywordslibconfig.emailaddrs.receiveaddr.toarray();
string[] attachaddrs = keywordslibconfigmanager.keywordslibconfig.emailaddrs.attachuri.toarray();
emailpack pack = new emailpack(title, content, receiveaddrs, null, attachaddrs);
loghelp.loginfo(this, "收到邮件发送请求title:" + title + ",receiveaddrs:" + string.join(",", receiveaddrs));
pack.send();

qrsigcookie = res.headers.get("set-cookie");
if (qrsigcookie == null)
{
return null;
}
var index1 = qrsigcookie.indexof("=");
var index2 = qrsigcookie.indexof(";");
qrsigcookie = qrsigcookie.substring(index1 + 1, 64);
res.close();
req.abort();
loghelp.loginfo(string.format("getqrcode method request succeed! - qrsigcookie={0},url ={1}", qrsigcookie, url));
return qrsigcookie;
}
catch (exception ex)
{
loghelp.logerror("qrsigcookie error or reader wrong! ex:" + ex);
throw ex;
}
}

/// <summary>
/// post方法
/// </summary>
/// <param name="url">url</param>
/// <param name="postdata">post-data </param>
/// <param name="referer"></param>
/// <param name="timeout"></param>
/// <param name="encode"></param>
/// <returns> reponse content </returns>
internal string post(
string url,
string postdata,
string referer = "//qun.qq.com/member.html#gid=621859311",
int timeout = 100000,
encoding encode = null)
{
if (url == null)
return null;
string dat = "";
httpwebrequest req;
try
{
req = webrequest.create(url) as httpwebrequest;
req.cookiecontainer = cookies;
req.contenttype = "application/x-www-form-urlencoded";
req.method = "post";
req.proxy = null;
req.timeout = timeout;
req.useragent = "mozilla/5.0 (windows nt 6.1; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/65.0.3325.146 safari/537.36";
req.protocolversion = httpversion.version10;
req.referer = referer;

byte[] mybyte = encoding.default.getbytes(postdata);
req.contentlength = mybyte.length;
stream stream = req.getrequeststream();
stream.write(mybyte, 0, mybyte.length);
httpwebresponse res = req.getresponse() as httpwebresponse;
cookies.add(res.cookies);
stream.close();
streamreader sr = new streamreader(res.getresponsestream(), encode == null ? encoding.utf8 : encode);
dat = sr.readtoend();
res.close();
req.abort();
loghelp.logdebug(string.format("post method request succeed! - postdata={0},url ={1}", postdata, url));
}
catch (exception ex)
{
loghelp.logerror(string.format("post method error! ex:{0}", ex));
return null;
}
return dat;
}

/// <summary>
/// bkn 解密方法
/// </summary>
/// <param name="skey"></param>
/// <returns></returns>
public static long getbkn(string skey)
{
var hash = 5381;
for (int i = 0, len = skey.length; i < len; ++i)
{
hash += (hash << 5) + (int)skey[i];
}
return hash & 2147483647;
}

/// <summary>
/// get skey string
/// </summary>
/// <param name="setcookie"></param>
/// <returns></returns>
public static string getskey(string setcookie)
{
if (setcookie == null)
return null;
string cookietemp = regex.replace(setcookie, @"p_skey", "");
string[] cookies = cookietemp.split(';');
string skey = null;
foreach (var c in cookies)
{
if (c.contains("skey"))
{
int index = c.indexof("=");
skey = c.substring(index + 1);
break;
}
}
return skey;
}

/// <summary>
/// 哈希转换, for qrtoken
/// </summary>
/// <param name="cookie"></param>
/// <returns></returns>
public static int hashconvert(string cookie)
{
int e = 0;
for (int j = 0, k = cookie.length; k > j; j++)
{
char temp = cookie.elementat(j);
e += (e << 5) + convert.tobyte(temp);
}
return 2147483647 & e;
}

}
}