模拟学信网登录,Cookie 序列化,在反序列化之后不能用的问题

昨天和今天在模拟学信网登录,然后抓取用户的信息数据的时候发现一直登录不成功,

登录页面,https://account.chsi.com.cn/passport/login?service=http%3A%2F%2Fmy.chsi.com.cn%2Farchive%2Fj_spring_cas_security_check

模拟学信网登录,Cookie 序列化,在反序列化之后不能用的问题

打开登录页面,发现就注入cookie了,然后自己也要模拟一个get请求,然后获取到cookie,

AccountModel accmodel = new AccountModel();
accmodel.username = "账号";
accmodel.password = "密码";
accmodel._eventId = "submit";
accmodel.submit = "登 录";
HttpHelper httphelper = new HttpHelper();

string strhtmlresult = httphelper.HttpGet(LoginIndexURL);
Regex regexlt = new Regex("<input type="hidden" name="lt" value="(?<hiddenlt>[^"]*?)"");

///携带验证标识
if (regexlt.IsMatch(strhtmlresult))
{
accmodel.lt = regexlt.Match(strhtmlresult).Groups["hiddenlt"].Value;
}


//获取cookie
List<Cookie> cookielist = new List<Cookie>();
foreach (Cookie cookie in httphelper.reponsecookie)
{
cookielist.Add(cookie);
}
httphelper.cookielist=cookielist;

#region 开始模拟cookie 请求
//model转换为dic,顺便把为空为null的值删掉
IDictionary<string, string> dicaccmodle = ModelUtil<AccountModel>.CreateDataALLdic_String(accmodel,true);
//HttpHelper httphelper = new HttpHelper();
//注入cookie
foreach (Cookie cookie in cookielist)
{
//cookielist.Add(new CookieModel() { Domain = cookie.Domain, Name = cookie.Name, Path = cookie.Path, Value = cookie.Value });
httphelper.requestcookie.Add(cookie);
}

strhtmlresult = httphelper.HttpPost(LoginIndexURL, SoleFupaySignature.GetSignContentbEncode_NoSort(dicaccmodle), LoginIndexURL);


Regex regexSchoolInformation = new Regex("a href="(?<SchoolInformationURL>[^"]*?)">学籍信息");
if (regexSchoolInformation.IsMatch(strhtmlresult))
{
string strSchoolInformationURL = regexSchoolInformation.Match(strhtmlresult).Groups["SchoolInformationURL"].Value;
string strhtmlSchoolInformation = httphelper.HttpGet(strSchoolInformationURL);

return new RedirectResult("/home/LoginSucess");
}
else
{
///携带验证标识
if (regexlt.IsMatch(strhtmlresult))
{
accmodel.lt = regexlt.Match(strhtmlresult).Groups["hiddenlt"].Value;
}
Regex regexcaptcha = new Regex("<input type="text" class='input_text' id='captcha'");

///是否出现验证码
if (regexcaptcha.IsMatch(strhtmlresult))
{
ViewBag.captcha = true;
}
}



#endregion

这样发现成功了

而把get和post分开放到另外一个方法中,用一个序列化的cookie在反序列化就不行了。

AccountModel accmodel = new AccountModel();
accmodel.username = "账号";
accmodel.password = "密码";
accmodel._eventId = "submit";
accmodel.submit = "登 录";
HttpHelper httphelper = new HttpHelper();

string strhtmlresult = httphelper.HttpGet(LoginIndexURL);
Regex regexlt = new Regex("<input type="hidden" name="lt" value="(?<hiddenlt>[^"]*?)"");

///携带验证标识
if (regexlt.IsMatch(strhtmlresult))
{
accmodel.lt = regexlt.Match(strhtmlresult).Groups["hiddenlt"].Value;
}


//获取cookie
List<Cookie> cookielist = new List<Cookie>();
foreach (Cookie cookie in httphelper.reponsecookie)
{
cookielist.Add(cookie);
}
httphelper.cookielist=cookielist;

  Login1(accmodel, Newtonsoft.Json.JsonConvert.SerializeObject(cookielist));

     }

public void Login1(AccountModel accmodel, string strcookielist)
{
List<Cookie> cookielist = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Cookie>>(strcookielist);
IDictionary<string, string> dicaccmodle = ModelUtil<AccountModel>.CreateDataALLdic_String(accmodel, true);
HttpHelper httphelper = new HttpHelper();// Newtonsoft.Json.JsonConvert.DeserializeObject<HttpHelper>(strhttphelper);
foreach (Cookie cookie in cookielist)
{
httphelper.requestcookie.Add(cookie);
}
string strhtmlresult = httphelper.HttpPost(LoginIndexURL, SoleFupaySignature.GetSignContentbEncode_NoSort(dicaccmodle), LoginIndexURL);
}

模拟学信网登录,Cookie 序列化,在反序列化之后不能用的问题

发现失败了, 后面自己想了下,可能由于cookie序列化后不允许传递给另外一个对象,所以失败了,然后就在根据原来的cookie,然后new 一个,传递给list

把Login1 换成下面的就可以了。

public void Login1(AccountModel accmodel, string strcookielist)
{
List<Cookie> cookielist = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Cookie>>(strcookielist);
IDictionary<string, string> dicaccmodle = ModelUtil<AccountModel>.CreateDataALLdic_String(accmodel, true);
HttpHelper httphelper = new HttpHelper();// Newtonsoft.Json.JsonConvert.DeserializeObject<HttpHelper>(strhttphelper);
foreach (Cookie cookie in cookielist)
{
Cookie newcookie = new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain);
newcookie.Secure = true;
newcookie.HttpOnly = true;
httphelper.requestcookie.Add(newcookie);
}
string strhtmlresult = httphelper.HttpPost(LoginIndexURL, SoleFupaySignature.GetSignContentbEncode_NoSort(dicaccmodle), LoginIndexURL);
}

然后这个是部署在服务器上面的,如果请求多的话会出现验证码,然后这样登录,如果去失败验证码。