需求是这样子的,想开发一个外挂程序,可以抓取别的系统的数据,从而实现数据验证。html
好比这样一个界面:ajax
使用Chrome浏览器分析http请求和响应过程以及页面的html代码,发现这是一个ajax请求,因而跟踪找到了具体的请求地址和查询时提交的数据。json
因而就能够请求这个地址,而且封装提交的数据进行http请求便可。浏览器
但实验后发现,须要先登陆系统而后才能进行查询请求。cookie
分析系统登陆部分代码发现,仍然是一个ajax post请求后台的代码,截图以下:app
从js代码能够看出res=899为登陆失败,其它为登陆成功。post
因而思路就肯定了,先模拟登录系统,而后使用相同的cookie,再次请求查询便可得到数据。url
登陆方法:spa
public static string PostLogin(string postData, string requestUrlString, ref CookieContainer cookie) { UTF8Encoding encoding = new UTF8Encoding(); byte[] data = encoding.GetBytes(postData); //向服务端请求 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString); myRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36"; myRequest.Method = "POST"; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.ContentLength = data.Length; myRequest.CookieContainer = new CookieContainer(); myRequest.AllowAutoRedirect = true; Stream newStream = myRequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); //将请求的结果发送给客户端(界面、应用) HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); cookie.Add(myResponse.Cookies); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); return reader.ReadToEnd(); }
登陆进系统后查询方法:code
public static string PostRequest(string postData, string requestUrlString, CookieContainer cookie) { UTF8Encoding encoding = new UTF8Encoding(); byte[] data = encoding.GetBytes(postData); HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString); myRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36"; myRequest.Method = "POST"; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.ContentLength = data.Length; myRequest.CookieContainer = cookie; myRequest.AllowAutoRedirect = true; Stream newStream = myRequest.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); return reader.ReadToEnd(); }
调用部分代码:
CookieContainer cc = new CookieContainer(); string url_login = "http://10.77.197.23:7001/yzjy/login.action?method=login1"; string postData_login = "submitData={\"username\":\"登陆帐号\",\"userpwd\":\"密码\"}"; string result_login = PostLogin(postData_login, url_login, ref cc); if (result_login.Equals("1748"))//1748表示登陆成功 { string url_getRyData = "http://10.77.197.23:7001/yzjy/Rygl.do?method=getRyData"; string postData_RyData = "aac002=" + sfz + "&aac003=" + xm + "&pageIndex=0&pageSize=30"; string result_RyData = PostRequest(postData_RyData, url_getRyData, cc); RyData ry = JsonConvert.DeserializeObject<RyData>(result_RyData); if (ry.total <= 0) { MessageBox.Show("对不起,没有查找到当前人信息。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } }
返回json数据,封装类的代码:
public class RyData { public int total { get; set; } public Data[] data { get; set; } } public class Data { public string aac161_name { get; set; } public string tbr { get; set; } public string aac161 { get; set; } public string aae100 { get; set; } public string czdz { get; set; } public string aac001 { get; set; } public string aac002 { get; set; } public string aae005 { get; set; } public string aac003 { get; set; } public string aac004 { get; set; } public string aac005 { get; set; } public string aac006 { get; set; } public string aac009_name { get; set; } public string aac009 { get; set; } public string aac005_name { get; set; } public string hjdz { get; set; } public string aac011_name { get; set; } public string aae011_name { get; set; } public string aae036 { get; set; } public string aac058 { get; set; } public string aac016 { get; set; } public string aac016_name { get; set; } public string aac004_name { get; set; } public string aac058_name { get; set; } public string aac024_name { get; set; } public string rn { get; set; } }
参考资料:
http://www.cnblogs.com/ok519/p/3488091.html