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

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

打开登录页面,发现就注入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,然后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);
}

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

模拟学信网登录,Cookie 序列化,在反序列化之后不能用的问题的更多相关文章

  1. java okhhtp下载学信网学籍信息

    学信网的登录有验证码,是那种计算数字或者汉字识别的,很难识别.最近连学籍信息和学历信息也换成图片了,常规的正则 css xpath都不能使. 下载图片,需要先登录,获取登陆后的cookie和学籍信息的 ...

  2. 学信网改绑手机号码,但是忘记了老号码怎么办?利用node.js + puppeteer 跑脚本实现改绑手机号

    最近登录学信网发现自己学信网上绑定的手机号码不是目前自己使用的手机号码,于是想改绑手机号,但是发现不记得之前的手机号码了: 于是百度各种方法都无济于事:也不想重新注册账号,最后看见一篇文章通过Pyth ...

  3. WebAPI调用笔记 ASP.NET CORE 学习之自定义异常处理 MySQL数据库查询优化建议 .NET操作XML文件之泛型集合的序列化与反序列化 Asp.Net Core 轻松学-多线程之Task快速上手 Asp.Net Core 轻松学-多线程之Task(补充)

    WebAPI调用笔记   前言 即时通信项目中初次调用OA接口遇到了一些问题,因为本人从业后几乎一直做CS端项目,一个简单的WebAPI调用居然浪费了不少时间,特此记录. 接口描述 首先说明一下,基于 ...

  4. 模拟登陆115网盘(MFC版)

    [cpp] view plain copy // 模拟登陆115网盘 #include <afxinet.h> // 包含相关的头文件 /* 用抓包工具抓包可得到需要提交的数据,然后模拟提 ...

  5. c#获取新浪微博登录cookie

    用新浪微博api收集数据有诸多限制,每小时只能调用官方api函数150次,认证也很麻烦.因此想通过爬网页的方式来收集数据.访问新浪微博用户网页首先需要登录,登录获取cookie后可直接获取网页数据,无 ...

  6. SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能

    在 Spring Security 中基于表单的认证模式,默认就是密码帐号登录认证,那么对于短信验证码+登录的方式,Spring Security 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...

  7. Spring Security构建Rest服务-1203-Spring Security OAuth开发APP认证框架之短信验证码登录

    浏览器模式下验证码存储策略 浏览器模式下,生成的短信验证码或者图形验证码是存在session里的,用户接收到验证码后携带过来做校验. APP模式下验证码存储策略 在app场景下里是没有cookie信息 ...

  8. Spring Security构建Rest服务-0702-短信验证码登录

    先来看下 Spring Security密码登录大概流程,模拟这个流程,开发短信登录流程 1,密码登录请求发送给过滤器 UsernamePasswordAuthenticationFilter 2,过 ...

  9. springboot +spring security4 自定义手机号码+短信验证码登录

    spring security 默认登录方式都是用户名+密码登录,项目中使用手机+ 短信验证码登录, 没办法,只能实现修改: 需要修改的地方: 1 .自定义 AuthenticationProvide ...

随机推荐

  1. 基于XMPP的即时通信系统的建立(四)— 协议详解

    Presence 在XMPP协议中,我们使用presence来获取用户是否已经上线以及是否可以通信的状态. 为了能够知道自己联系人的状态以及让联系人知道自己的状态,用户上线后需要订阅联系人的状态,联系 ...

  2. mybatis源码分析(1)——SqlSessionFactory实例的产生过程

    在使用mybatis框架时,第一步就需要产生SqlSessionFactory类的实例(相当于是产生连接池),通过调用SqlSessionFactoryBuilder类的实例的build方法来完成.下 ...

  3. 玩转EasyUi弹出框

    这两天在搞EasyUi的弹出框,弹出框之前也搞过很多个版本,总是觉得不那么完美,刚好最近有时间,就往多处想了想,功能基本上达到我的预期,并且在开发过程中遇到很多小技巧,特撰文如下. 走起:在EasyU ...

  4. mysql if 和 case when 用法 多个when情况用一个语句 存储过程

    在实际开发中,经常会用到 if 和 case when的用法,记录一下,以后可以用得到. DELIMITER $$ USE `数据库`$$ DROPPROCEDUREIFEXISTS `GetNoti ...

  5. Web Api 中使用 PCM TO WAV 的语音操作

    /// <summary> /// 语音[文件.上传.解码.保存(WAV)] /// </summary> [DeveloperEx("Liwei:秘书语音需求单&q ...

  6. UVA 11478 Halum(用bellman-ford解差分约束)

    对于一个有向带权图,进行一种操作(v,d),对以点v为终点的边的权值-d,对以点v为起点的边的权值+d.现在给出一个有向带权图,为能否经过一系列的(v,d)操作使图上的每一条边的权值为正,若能,求最小 ...

  7. 【C#学习笔记】写文件

    using System; using System.IO; namespace ConsoleApplication { class Program { static void Main(strin ...

  8. 【转】Android fill_parent和wrap_content分析

    fill_parent设置一个顶部布局或控件强制性让它布满整个屏幕. wrap_content布局指根据视图内部内容自动扩展以适应其大小. 1. wrap_content <?xml versi ...

  9. android 应用页面与数据申请逻辑剥离;

    1.页面与数据申请剥离,数据申请框架可以灵活更换,解耦合: 2.对应页面的数据申请类中,将返回数据解析剥离,灵活更换数据返回及对应解析: 二.模块划分: 1.一些通用的工具类,可以考虑迁移到com.c ...

  10. POJ 1160 Post Office

    题意:有n个村庄,要在其中m个村庄里建邮局,每个村庄去邮局的代价为当前村庄到最近的一个有邮局村庄的路程,问总最小代价是多少. 解法:dp.dp[i][j]表示在前j个村庄建立i个邮局后的代价,则状态转 ...