[C#] 纯文本查看 复制代码
class QQHelper2
{
HttpHelper _HttpHelper;
public QQHelper2()
{
_HttpHelper = new HttpHelper();
HttpCookie = new CookieCollection();
}
public static string GetRandrom(int number)
{
Random rand = new Random();
string strNum = "";
int i = 0;
while (i < number)
{
i++;
int randomNum = rand.Next(0, 10);
strNum += randomNum.ToString();
}
strNum = "0." + strNum;
return strNum;
}
public static string GetBetween(string s, string s1, string s2)
{
int n1, n2;
n1 = s.IndexOf(s1, 0) + s1.Length; //开始位置
n2 = s.IndexOf(s2, n1); //结束位置
return s.Substring(n1, n2 - n1); //取搜索的条数,用结束的位置-开始的位置,并返回
}
private string QQNumber;
public string _QQNumber
{
get { return QQNumber; }
set { QQNumber = value; }
}
private string QQPassword;
public string _QQPassword
{
get { return QQPassword; }
set { QQPassword = value; }
}
private string VeryCode;
public string _VeryCode
{
get { return VeryCode; }
set { VeryCode = value; }
}
private string ErrMessage;
public string _ErrMessage
{
get { return ErrMessage; }
set { ErrMessage = value; }
}
#region 登录变量
private HttpItem _HttpItem;
private HttpResult _HttpResult;
private string login_sig;
private string Ptuin;
private string sid;
private CookieCollection HttpCookie;
#endregion
/// <summary>
/// 判断是否需要验证码0表示不需要,-1表示需要,-2表示出错误
/// </summary>
/// <returns>0表示不需要,-1表示需要,-2表示出错误</returns>
///
public int CheckVerycode()
{
//try
//{
#region 获取login_sig,Cookie
_HttpItem = new HttpItem
{
Allowautoredirect = false,
URL = "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_input1f1a66.css",
Encoding = Encoding.UTF8,
IsToLower = false,
Method = "get",
UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
ContentType = "application/x-www-form-urlencoded",
ResultType = ResultType.String,
Timeout = 50000,
ResultCookieType = ResultCookieType.CookieCollection
};
_HttpResult = _HttpHelper.GetHtml(_HttpItem);
login_sig = QQHelper2.GetBetween(_HttpResult.Html, "login_sig:\"", "\",clientip:");
HttpCookie.Add( _HttpResult.CookieCollection);
_HttpItem = new HttpItem
{
Allowautoredirect = false,
URL = string.Format("https://ssl.ptlogin2.qq.com/check?regmaster=&uin={0}&appid=522005705&js_ver=10085&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}",
QQNumber, login_sig, QQBottleHelper.GetRandrom(18)),
Encoding = Encoding.UTF8,
IsToLower = false,
Method = "get",
UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
ContentType = "application/x-www-form-urlencoded",
ResultType = ResultType.String,
Timeout = 50000,
ResultCookieType = ResultCookieType.CookieCollection,
CookieCollection=HttpCookie
};
_HttpResult = _HttpHelper.GetHtml(_HttpItem);
HttpCookie.Add(_HttpResult.CookieCollection);
string[] ReturnNumber = _HttpResult.Html.Split(new string[] { "','", "', '" }, StringSplitOptions.None);
VeryCode = ReturnNumber[1];
Ptuin = ReturnNumber[2];
if (VeryCode.Length > 4)
{
return -1;
}
else
{
return 0;
}
#endregion
//}
//catch (Exception er)
//{
// ErrMessage = er.Message;
// return -2;
//}
}
#region 获取验证码图片
public Image GetVeryCode()
{
_HttpItem = new HttpItem
{
Allowautoredirect = false,
URL = string.Format("https://ssl.captcha.qq.com/getimage?uin={0}&aid=522005705&cap_cd={1}&{2}",
QQNumber, VeryCode, QQBottleHelper.GetRandrom(17)),
Encoding = Encoding.UTF8,
IsToLower = false,
Method = "get",
UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
ContentType = "application/x-www-form-urlencoded",
ResultType = ResultType.Byte,
Timeout = 50000,
ResultCookieType = ResultCookieType.CookieCollection,
CookieCollection=HttpCookie
};
_HttpResult = _HttpHelper.GetHtml(_HttpItem);
HttpCookie.Add(_HttpResult.CookieCollection);
return byteArrayToImage(_HttpResult.ResultByte);
}
#endregion
public int LoginQQMail()
{
try
{
string sScript = Properties.Resources.QQMailPasswordJs;
sScript = sScript.Replace("ptuin", Ptuin);
ScriptEngine se = new ScriptEngine(ScriptLanguage.JavaScript);
object[] paramers = new object[2] { QQPassword, VeryCode };
object obj = se.Run("getEncryption", paramers, sScript);
QQPassword = obj.ToString();
_HttpItem = new HttpItem
{
Allowautoredirect = false,
URL = string.Format("https://ssl.ptlogin2.qq.com/login?u={0}&verifycode={1}&p={2}&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=2-74-1406134725843&js_ver=10085&js_type=1&login_sig={3}&pt_uistyle=25&aid=522005705&daid=4&",
QQNumber, VeryCode, QQPassword, login_sig),
Encoding = Encoding.UTF8,
IsToLower = false,
Method = "get",
UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
ContentType = "application/x-www-form-urlencoded",
ResultType = ResultType.String,
Timeout = 50000,
ResultCookieType = ResultCookieType.CookieCollection,
CookieCollection=HttpCookie
};
_HttpResult = _HttpHelper.GetHtml(_HttpItem);
HttpCookie.Add(_HttpResult.CookieCollection);
if (_HttpResult.Html.Contains("验证码"))
{
ErrMessage = "错误:验证码输入错误";
return -2;
}
if (_HttpResult.Html.Contains("密码"))
{
ErrMessage = "错误:密码或者用户名输入错误";
return -3;
}
ErrMessage = _HttpResult.Html;
//获取Location和cOOKIE
string[] ReturnNumber = _HttpResult.Html.Split(new string[] { "','", "', '" }, StringSplitOptions.None);
_HttpItem = new HttpItem
{
Allowautoredirect = false,
URL = ReturnNumber[2],
Encoding = Encoding.GetEncoding("gb18030"),
IsToLower = false,
Method = "get",
UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
ContentType = "application/x-www-form-urlencoded",
ResultType = ResultType.String,
Timeout = 50000,
ResultCookieType = ResultCookieType.CookieCollection,
CookieCollection = HttpCookie
};
_HttpResult = _HttpHelper.GetHtml(_HttpItem);
HttpCookie.Add(_HttpResult.CookieCollection);
ErrMessage = _HttpResult.Html;
_HttpItem = new HttpItem
{
Allowautoredirect = false,
URL = _HttpResult.Header["Location"],
Encoding = Encoding.GetEncoding("gb18030"),
IsToLower = false,
Method = "get",
UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
ContentType = "application/x-www-form-urlencoded",
ResultType = ResultType.String,
Timeout = 50000,
ResultCookieType = ResultCookieType.CookieCollection,
CookieCollection = HttpCookie
};
_HttpResult = _HttpHelper.GetHtml(_HttpItem);
HttpCookie.Add(_HttpResult.CookieCollection);
ErrMessage = _HttpResult.Html;
_HttpItem = new HttpItem
{
Allowautoredirect = false,
URL = _HttpResult.Header["Location"],
Encoding = Encoding.GetEncoding("gb18030"),
IsToLower = false,
Method = "get",
UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
ContentType = "application/x-www-form-urlencoded",
ResultType = ResultType.String,
Timeout = 50000,
ResultCookieType = ResultCookieType.CookieCollection,
CookieCollection = HttpCookie
};
_HttpResult = _HttpHelper.GetHtml(_HttpItem);
HttpCookie.Add(_HttpResult.CookieCollection);
ErrMessage = _HttpResult.Html;
return 0;
}
catch
{
return 0;
}
}
private Image byteArrayToImage(byte[] Bytes)
{
MemoryStream ms = new MemoryStream(Bytes);
Image outputImg = Image.FromStream(ms);
return outputImg;
}
}