http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 19010|回复: 5

[其他] 求助模拟QQ登录,并获取好友与群信息

[复制链接]
发表于 2014-7-1 13:55:28 | 显示全部楼层 |阅读模式
10金钱
今天组长给个任务,估计是为了推广产品用.
要求大概是:
1.创建一个web程序,模拟QQ登录,获取个人信息。
2.获取好友信息
3.获取群信息。
求助,没做过此类程序.
Asp.net的.
哪有类似的源码.比如QQ机器人web版本的.



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-7-1 13:57:00 | 显示全部楼层
这个不好实现,没程序,有也是一以前的,可以参考下。你直接搜索,QQ登录
回复

使用道具 举报

发表于 2014-7-1 14:06:43 | 显示全部楼层
[C#] 纯文本查看 复制代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace QQLogin
{
    public class Login
    {
        public static string referer = "https://xui.ptlogin2.qq.com/cgi-bin/xlogin?appid=522005705&daid=4&s_url=https://mail.qq.com/cgi-bin/login?vt=passport%26vm=wpt%26ft=loginpage%26target=&style=25&low_login=1&proxy_url=https://mail.qq.com/proxy.html&need_qr=0&hide_border=1&border_radius=0&self_regurl=http://zc.qq.com/chs/index.html?type=1&app_id=11005?t=regist&pt_feedback_link=http://support.qq.com/discuss/350_1.shtml&css=https://res.mail.qq.com/zh_CN/htmledition/style/ptlogin_input1dd8c7.css";
        /// <summary>
        /// 初始化Cookie
        /// </summary>
        /// <param name="tbCookieCollection">cookie</param>
        public static string InitDefaultCookie(CookieCollection cookieCollection)
        {
            string accept = "text/html, application/xhtml+xml, */*";
            string treferer = "https://mail.qq.com/cgi-bin/loginpage";
            HttpHelper helper = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = referer,
                Accept = accept,
                Referer = treferer,
                ResultCookieType = ResultCookieType.CookieCollection
            };
            HttpResult result = helper.GetHtml(item);
            cookieCollection.Add(result.CookieCollection);

            string login_sig = Regex.Match(result.Html, "(?is)(?<=login_sig\":\")[^\"]+(?=\")").Value;
            return login_sig;
        }
        /// <summary>
        /// 判断是否需要验证码
        /// </summary>
        /// <param name="username">QQ账号</param>
        /// <param name="cookieCollection">Cokie</param>
        /// <returns>false/需要验证码 其他为默认验证码</returns>
        public static Hashtable CheckIfNeedCode(string username, string login_sig, CookieCollection cookieCollection)
        {
            Hashtable ht = new Hashtable();
            Random r = new Random();
            string url = string.Format("https://ssl.ptlogin2.qq.com/check?regmaster=&uin={0}&appid=522005705&js_ver=10082&js_type=1&login_sig={1}&u1=https%3A%2F%2Fmail.qq.com%2Fcgi-bin%2Flogin%3Fvt%3Dpassport%26vm%3Dwpt%26ft%3Dloginpage%26target%3D&r={2}", username, login_sig,r.NextDouble());
            
            HttpHelper helper = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = url,
                Accept = "application/javascript, */*;q=0.8",
                Referer = referer,
                ResultCookieType = ResultCookieType.CookieCollection,
                CookieCollection = cookieCollection
            };
            HttpResult result = helper.GetHtml(item);
            cookieCollection.Add(result.CookieCollection);
            string retString = result.Html;
            try
            {
                //得验证吗 
                if (retString.Contains("ptui_checkVC('0','"))
                {
                    ht.Add("state",false);
                    ht.Add("code", retString.Replace("ptui_checkVC('0','", "").Replace("'", "").Replace(")", "").Replace(";", "").Substring(0, 4));
                    //不需要手动输入 
                }
                else
                {
                    ht.Add("state",true);
                    retString = retString.Replace("ptui_checkVC(", "").Replace(")", "");
                    string[] data = retString.Split(new char[] { ',' });
                    ht.Add("code",data[1].Replace("'",""));
                }
                return ht;
            }
            catch
            {
                ht.Add("state", true);
                return ht;
            }
        }
        /// <summary>
        /// 获取验证码图片的二进制值
        /// </summary>
        /// <param name="cap_cd"></param>
        /// <returns></returns>
        public static byte[] getVerfycodeImage(string username, string cap_cd, CookieCollection cookieCollection)
        {
            Random rand = new Random();
            string url = string.Format("https://ssl.captcha.qq.com/getimage?uin={0}&aid=522005705&cap_cd={1}&{2}", username, cap_cd,rand.NextDouble());
            HttpHelper help = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = url,
                ResultType = ResultType.Byte,
                ResultCookieType = ResultCookieType.CookieCollection,
                CookieCollection = cookieCollection
            };
            HttpResult result = help.GetHtml(item);
            cookieCollection.Add(result.CookieCollection);
            return result.ResultByte;
        }
        /// <summary>
        /// 正式登陆
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="code"></param>
        /// <param name="login_sig"></param>
        /// <param name="cookieCollection"></param>
        /// <returns></returns>
        public static Hashtable login(string username, string password, string code, string login_sig, CookieCollection cookieCollection)
        {
            Hashtable ht = new Hashtable();
            StringBuilder sb = new StringBuilder();
            sb.Append("https://ssl.ptlogin2.qq.com/login?");
            sb.Append("u=").Append(username).Append("&");
            sb.Append("verifycode=").Append(code).Append("&");
            sb.Append("p=").Append(PasswordHelper.GetPassword(username,password,code)).Append("&");
            sb.Append("login_sig=").Append(login_sig).Append("&");
            sb.Append(string.Format("&pt_rsa=0&u1=https%3A%2F%2Fmail.qq.com%2Fcgi-bin%2Flogin%3Fvt%3Dpassport%26vm%3Dwpt%26ft%3Dloginpage%26target%3D%26account%3D{0}&ptredirect=1&h=1&t=1&g=1&from_ui=1&ptlang=2052&action=&js_ver=10082&js_type=1&pt_uistyle=25&aid=522005705&daid=4&",username));
            HttpHelper help = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = sb.ToString(),
                Accept = "application/javascript, */*;q=0.8",
                Referer = referer,
                CookieCollection = cookieCollection,
                ResultCookieType = ResultCookieType.CookieCollection
            };
            HttpResult result = help.GetHtml(item);
            cookieCollection.Add(result.CookieCollection);
            string retString = result.Html;
            ht.Add("html1", retString);
            try
            {
                if (retString.Contains("登录成功"))
                {
                    ht.Add("nick", Regex.Match(retString, "(?is)(?<=登录成功!', ')[^']+(?=')").Value);
                    retString = retString.Replace("ptuiCB(", "").Replace(")","");
                    string[] data = retString.Split(new char[]{','});
                    string url = data[2].Replace("'","");
                    item = new HttpItem()
                    {
                        URL = url,
                        Accept = "text/html, application/xhtml+xml, */*",
                        Referer = referer,
                        CookieCollection = cookieCollection,
                        ResultCookieType = ResultCookieType.CookieCollection
                    };
                    result = help.GetHtml(item);
                    cookieCollection.Add(result.CookieCollection);

                    //这里要捕获302跳转
                    string wburl = result.Header["Location"];
                    while (!string.IsNullOrWhiteSpace(wburl))
                    {
                        item = new HttpItem()
                        {
                            URL = wburl,
                            ResultCookieType = ResultCookieType.CookieCollection,
                            CookieCollection = cookieCollection
                        };
                        result = help.GetHtml(item);
                        cookieCollection.Add(result.CookieCollection);
                        wburl = result.Header["Location"];
                    }
                    string sid = Regex.Match(result.Html, "(?is)(?<=sid=)[^\"]+(?=\")").Value;
                    ht.Add("html",result.Html);
                    ht.Add("state", true);
                    ht.Add("sid",sid);
                    ht.Add("msg", "登陆成功");
                }
                else
                {
                    ht.Add("state", false);
                    ht.Add("msg", string.IsNullOrWhiteSpace(Regex.Match(retString, "[\u4e00-\u9fa5]+").Value) ? "连接超时,请重试!" : Regex.Match(retString, "[\u4e00-\u9fa5]+").Value);
                }
            }
            catch
            {
                ht.Add("state", false);
                ht.Add("msg", string.IsNullOrWhiteSpace(Regex.Match(retString, "[\u4e00-\u9fa5]+").Value) ? "连接超时,请重试!" : Regex.Match(retString, "[\u4e00-\u9fa5]+").Value);
            }
            
            return ht;
        }
        
        #region 时间转换成时间戳
        /// <summary>
        /// 将时间转换成时间戳
        /// </summary>
        /// <returns></returns>
        public static string UnixStamp(DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));

            long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位
            return t.ToString();
        }
        #endregion
    }
}
回复

使用道具 举报

发表于 2014-7-1 14:07:53 | 显示全部楼层
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace QQLogin
{
    class PasswordHelper
    {
        /// <summary> 
        /// 根据QQ号码和验证码加密密码 
        /// </summary> 
        /// <param name="qqNum">QQ号码</param> 
        /// <param name="password">QQ密码</param> 
        /// <param name="verifycode">验证码</param> 
        /// <returns>密码密文</returns> 
        public static string GetPassword(string qqNum, string password, string verifycode)
        {
            //uin为QQ号码转换为16位的16进制 
            int qq;
            int.TryParse(qqNum, out qq);
            
            qqNum = qq.ToString("x");
            qqNum = qqNum.PadLeft(16, '0');

            String P = hexchar2bin(md5(password));
            String U = md5(P + hexchar2bin(qqNum)).ToUpper();
            String V = md5(U + verifycode.ToUpper()).ToUpper();
            return V;
        }

        public static string md5(string input)
        {
            byte[] buffer = MD5.Create().ComputeHash(Encoding.GetEncoding("ISO-8859-1").GetBytes(input));
            return binl2hex(buffer);
        }

        public static string binl2hex(byte[] buffer)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < buffer.Length; i++)
            {
                builder.Append(buffer[i].ToString("x2"));
            }
            return builder.ToString();
        }

        public static string hexchar2bin(string passWord)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < passWord.Length; i = i + 2)
            {
                builder.Append(Convert.ToChar(Convert.ToInt32(passWord.Substring(i, 2), 16)));
            }
            return builder.ToString();
        }
    }
}
回复

使用道具 举报

发表于 2014-7-1 14:11:38 | 显示全部楼层
[C#] 纯文本查看 复制代码

使用方法:
//新建Cookie
cookieCollection = new CookieCollection();
string login_sig = Login.InitDefaultCookie(cookieCollection);

//判断是否需要验证码
Hashtable ht = Login.CheckIfNeedCode(username, login_sig, cookieCollection);

bool state = Boolean.Parse(ht["state"].ToString());
string cap_cd = ht["code"].ToString();

//需要验证码
if (state)
{
while (true)
{
    this.BeginInvoke(updateLog, "需要验证码,正在查询……");
    Hashtable imght = VerfycodeImage(username, cap_cd);
    bool imgState = Boolean.Parse(imght["state"].ToString());
    if (imgState)
    {
        code = imght["code"].ToString();
        codenum = imght["codenum"].ToString();
        this.BeginInvoke(updateLog, "验证码查询成功:" + code);
        break;
    }
    else
    {
        this.BeginInvoke(updateLog,imght["msg"].ToString());
        Thread.Sleep(2000);
    }
}

}
else
{
code = cap_cd;
}

//开始登陆
Hashtable loginResult = Login.login(username, password, code, login_sig, cookieCollection);

bool loginState = Boolean.Parse(loginResult["state"].ToString());
回复

使用道具 举报

 楼主| 发表于 2014-7-1 15:56:34 | 显示全部楼层
非常感谢你帮了我的大忙,真的太感谢你啦!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-13 18:43

© 2014-2021

快速回复 返回顶部 返回列表