采集学信网源码(验证码用户自己输入)

  • 投稿人: anan
  • 发布时间: 2017-06-03 13:11:42
  • 阅读: 287
  • 收藏: 16
  • 点赞: 1
  • 评论: 0

using Dataacquisitionnetwork.App_Start;
using Dataacquisitionnetwork.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using _51sole_soulefu_Common;
using System.Net;
using System.Collections.Specialized;
using System.IO;
using System.Drawing;

namespace Dataacquisitionnetwork.Controllers
{
public class HomeController : Controller
{
public static Dictionary<string, string> dicarryxml = XMLDocument.readdic();
public ActionResult Index()
{
/*
string dssdsindex = "http://user.51sole.com/Default.aspx";
AccountModel accmodel = new AccountModel();
HttpHelper httphelper = new HttpHelper();
string strhtmlresult = httphelper.HttpGet(dssdsindex);
Regex regexlt = new Regex("<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"(?<hiddenlt>[^\"]*?)\"");
string __VIEWSTATE="";
///携带验证标识
if (regexlt.IsMatch(strhtmlresult))
{
__VIEWSTATE = regexlt.Match(strhtmlresult).Groups["hiddenlt"].Value;
}
IDictionary<string, string> dicaccmodle = new Dictionary<string, string>();
dicaccmodle.Add("__VIEWSTATE", __VIEWSTATE);
dicaccmodle.Add("txtUserName", "账号");
dicaccmodle.Add("txtPwd", "密码");
dicaccmodle.Add("btnLogin", "登录");

string strhtmlresult32323 = httphelper.HttpPost(dssdsindex, SoleFupaySignature.GetSignContentbEncode_NoSort(dicaccmodle), dssdsindex);
* */
ViewBag.Title = "Home Page";
return View();
}

public static string LoginIndexURL = "https://account.chsi.com.cn/passport/login?service=http%3A%2F%2Fmy.chsi.com.cn%2Farchive%2Fj_spring_cas_security_check";

//public static string LoginPostURL = "http://my.chsi.com.cn/archive/j_spring_cas_security_check";
public static string ImageCodeURL = "https://account.chsi.com.cn/passport/captcha.image?id=";

/// <summary>
/// 获取cookie和lt 随机密钥
/// </summary>
/// <param name="accmodel"></param>
/// <param name="cookielist"></param>
/// <returns></returns>
public AccountModel HttpGetCookie(AccountModel accmodel, out List<Cookie> cookielist)
{
HttpHelper httphelper = new HttpHelper();
string strhtmlresult = httphelper.HttpGet(LoginIndexURL);
Regex regexlt = new Regex(dicarryxml["regexlt"]);

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


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

}

[HttpGet]
public ActionResult Login()
{
AccountModel accmodel = new AccountModel();
//accmodel.username = "账号"; //
//accmodel.password = "密码"; //
accmodel._eventId = "submit";
accmodel.submit = "登 录";

List<Cookie> cookielist = new List<Cookie>();
HttpGetCookie(accmodel, out 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(dicarryxml["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
*/

//ViewBag.CookieValue = Newtonsoft.Json.JsonConvert.SerializeObject(cookielist);
//Login1(accmodel, Newtonsoft.Json.JsonConvert.SerializeObject(cookielist));
//strhtmlresult = httphelper.HttpPost(LoginIndexURL, SoleFupaySignature.GetSignContentbEncode_NoSort(dicaccmodle), LoginIndexURL);
ViewBag.CookieValue =Newtonsoft.Json.JsonConvert.SerializeObject(cookielist);
return View(accmodel);
}
/*
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);
}
*/
/// <summary>
/// post登录
/// </summary>
/// <param name="accmodel"></param>
/// <param name="CookieValue"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Login(AccountModel accmodel, string CookieValue)
{
//存储cookie
List<Cookie> cookielist = new List<Cookie>();

bool searchResult = false;
IDictionary<string, string> dicaccmodle = ModelUtil<AccountModel>.CreateDataALLdic_String(accmodel,true);
HttpHelper httphelper = new HttpHelper();

//注入cookie
if (!string.IsNullOrWhiteSpace(CookieValue))
{

List<Cookie> oldcookielist = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Cookie>>(CookieValue);
foreach (Cookie cookie in oldcookielist)
{
Cookie newcookie = new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain);
newcookie.Secure = true;
newcookie.HttpOnly = false;
httphelper.requestcookie.Add(newcookie);
}

//httphelper = Newtonsoft.Json.JsonConvert.DeserializeObject<HttpHelper>(CookieValue);

}
string strhtmlresult = httphelper.HttpPostByLocation(LoginIndexURL, SoleFupaySignature.GetSignContentbEncode_NoSort(dicaccmodle), LoginIndexURL);
ViewBag.captcha = false;
//判断存在学籍信息
Regex regexSchoolInformation = new Regex(dicarryxml["SchoolInformationURL"]);
if (regexSchoolInformation.IsMatch(strhtmlresult))
{
string strSchoolInformationURL = regexSchoolInformation.Match(strhtmlresult).Groups["SchoolInformationURL"].Value;
string strhtmlSchoolInformation = httphelper.HttpGet_NoResponseCookie(httphelper.LocationAuthority + strSchoolInformationURL, "", httphelper.LocationAbsoluteUri);
List<String> listresultTable = new List<string>();
//string resultTable = Comm.MatchComInfo(strhtmlSchoolInformation, dicarryxml["resultTable"], "resultTable");
Regex regex = new Regex(dicarryxml["resultTable"]);
MatchCollection matches = regex.Matches(strhtmlSchoolInformation);
foreach (Match match in matches)
{
string strresultTable = match.Groups["resultTable"].Value;
listresultTable.Add(strresultTable);
string Fullname = Comm.MatchComInfo(strresultTable, dicarryxml["Fullname"], "Fullname");
string Sex = Comm.MatchComInfo(strresultTable, dicarryxml["Sex"], "Sex");
string Ethnic = Comm.MatchComInfo(strresultTable, dicarryxml["Ethnic"], "Ethnic");
string IDcard = Comm.MatchComInfo(strresultTable, dicarryxml["IDcard"], "IDcard");
string School = Comm.MatchComInfo(strresultTable, dicarryxml["School"], "School");
string Educationallevel = Comm.MatchComInfo(strresultTable, dicarryxml["Educationallevel"], "Educationallevel");
string Learningprofession = Comm.MatchComInfo(strresultTable, dicarryxml["Learningprofession"], "Learningprofession");
string StudyYears = Comm.MatchComInfo(strresultTable, dicarryxml["StudyYears"], "StudyYears");
string Academiccategory = Comm.MatchComInfo(strresultTable, dicarryxml["Academiccategory"], "Academiccategory");
string LearningWays = Comm.MatchComInfo(strresultTable, dicarryxml["LearningWays"], "LearningWays");
string Schoolbranch = Comm.MatchComInfo(strresultTable, dicarryxml["Schoolbranch"], "Schoolbranch");
string Departmentof = Comm.MatchComInfo(strresultTable, dicarryxml["Departmentof"], "Departmentof");
string Schoolclass = Comm.MatchComInfo(strresultTable, dicarryxml["Schoolclass"], "Schoolclass");
string StudentID = Comm.MatchComInfo(strresultTable, dicarryxml["StudentID"], "StudentID");
string EnrollmentDate = Comm.MatchComInfo(strresultTable, dicarryxml["EnrollmentDate"], "EnrollmentDate");
string LeaveDate = Comm.MatchComInfo(strresultTable, dicarryxml["LeaveDate"], "LeaveDate");
string Studentstatus = Comm.MatchComInfo(strresultTable, dicarryxml["Studentstatus"], "Studentstatus");
}
searchResult = true;
ViewBag.listresultTable = listresultTable;

HttpGetCookie(accmodel, out cookielist);
}
else
{

Regex regexstatuserrors = new Regex(dicarryxml["regexlt"]);
if (regexstatuserrors.IsMatch(strhtmlresult))
{
string statuserrors = regexstatuserrors.Match(strhtmlresult).Value;
ViewBag.statuserrors = statuserrors;
}
else
{
// HttpGetCookie(accmodel, out cookielist);
}

string captchaerror = Comm.MatchComInfo(strhtmlresult, dicarryxml["captchaerror"], "captchaerror");
if (!string.IsNullOrWhiteSpace(captchaerror))
{
ViewBag.statuserrors = captchaerror;
}

Regex regexlt = new Regex(dicarryxml["regexlt"]);

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

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

}
ViewBag.CookieValue = Newtonsoft.Json.JsonConvert.SerializeObject(cookielist);
ViewBag.searchResult = searchResult;
return View(accmodel);
}
/// <summary>
/// 获取验证码图片
/// </summary>
/// <param name="CookieValue"></param>
/// <param name="ramid"></param>
/// <returns></returns>
public ActionResult CreateVerification(string CookieValue, string ramid)
{
ResponseModel reponsemodel = new ResponseModel();
try
{
//CookieValue = HttpUtility.UrlDecode(CookieValue);
if (string.IsNullOrWhiteSpace(ramid))
{
ramid = Comm.CreateGuidString();
}

HttpHelper httphelper = new HttpHelper();

List<Cookie> oldcookielist = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Cookie>>(CookieValue);
foreach (Cookie cookie in oldcookielist)
{
Cookie newcookie = new Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain);
newcookie.Secure = true;
newcookie.HttpOnly = false;
httphelper.requestcookie.Add(newcookie);
}

Stream resStream = httphelper.GetResponseImage(ImageCodeURL + ramid);

Bitmap image = new Bitmap(resStream);
string directory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "codeImage");
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
string image_path = Path.Combine(directory, Comm.GenerateOrderNumber() + ".jpg");

image.Save(image_path);

MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = ms.GetBuffer(); //byte[] bytes= ms.ToArray(); 这两句都可以,至于区别么,下面有解释
//ms.Close();
//File.Delete(image_path);
//Response.ClearContent();
//Response.Write(bytes);
return File(bytes, @"image/jpeg");
//reponsemodel.Code = (int)ResponseEnum.Sucess;
//reponsemodel.Body = VerificationCode;
}
catch (Exception ex)
{
reponsemodel.Code = (int)ResponseEnum.Exceptionerror;
reponsemodel.Msg = ex.Message;
return Json("");
}
// return Json(reponsemodel);

}
/// <summary>
/// 登录成功的提示页面
/// </summary>
/// <returns></returns>
public ActionResult LoginSucess()
{
return View();
}

 

 

}
}