[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
}
}