|
楼主 |
发表于 2013-10-24 18:03:06
|
显示全部楼层
每一次请求我都会把返回的cookie付给下次请求
下面是源码,3个out参数是登陆时的种子
private static HttpResult GetCode(out string loginHash, out string seccode, out string formHash)
{
// 请求登陆页,并获取有用参数信息
HttpItem item = new HttpItem()
{
URL = "http://bbs.pcbeta.com/member.php?mod=logging&action=login",
Method = "GET",
Host = "bbs.pcbeta.com",
Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0",
};
HttpHelper http = new HttpHelper();
HttpResult result = http.GetHtml(item);
Regex reg_Seccode = new Regex(@"(?<=seccodeverify_).*?(?="")", RegexOptions.Multiline | RegexOptions.IgnoreCase);
Regex reg_LoginHash = new Regex(@"(?<=loginhash=).*?(?="")", RegexOptions.Multiline | RegexOptions.IgnoreCase);
Regex reg_FormHash = new Regex(@"(?<=name=""formhash"" value="").*?(?="")", RegexOptions.Multiline | RegexOptions.IgnoreCase);
// 3个参数
loginHash = reg_LoginHash.Match(result.Html).Value;
seccode = reg_Seccode.Match(result.Html).Value;
formHash = reg_FormHash.Match(result.Html).Value;
// 获取验证码,第一次请求
item.URL = string.Format("http://bbs.pcbeta.com/misc.php?mod=seccode&action=update&idhash={0}&inajax=1&ajaxtarget=seccode_{0}", seccode);
item.Cookie = result.Cookie;
result = http.GetHtml(item);
Regex reg_Update = new Regex(@"(?<=update=).*?(?=&)", RegexOptions.Multiline | RegexOptions.IgnoreCase);
string update = reg_Update.Match(result.Html).Value;
// 获取验证码,第二次请求
item.URL = string.Format("http://bbs.pcbeta.com/misc.php?mod=seccode&update={0}&idhash={1}", update, seccode);
item.Accept = "image/png,image/*;q=0.8,*/*;q=0.5";
item.Referer = "http://bbs.pcbeta.com/member.php?mod=logging&action=login";
item.ResultType = ResultType.Byte;
item.Cookie = result.Cookie;
result = http.GetHtml(item);
// 验证码本地输出
ImageHelper.ConvertByteToImg(result.ResultByte, @"D:\a.png");
// 验证码验证请求,需要将验证码附到url后面
item.URL = "http://bbs.pcbeta.com/misc.php?mod=seccode&action=check&inajax=1&&idhash=" + seccode + "&secverify=" + "";
item.Referer = "http://bbs.pcbeta.com/member.php?mod=logging&action=login";
item.Header.Add("x-requested-with", "XMLHttpRequest");
item.Cookie = result.Cookie;
result = http.GetHtml(item);
return result;
}
|
|