首页 > 代码库 > C#模拟登录后请求查询
C#模拟登录后请求查询
需求是这样子的,想开发一个外挂程序,能够抓取别的系统的数据,从而实现数据验证。
比如这样一个界面:
使用Chrome浏览器分析http请求和响应过程以及页面的html代码,发现这是一个ajax请求,于是跟踪找到了具体的请求地址和查询时提交的数据。
于是就可以请求这个地址,并且封装提交的数据进行http请求即可。
但实验后发现,需要先登录系统然后才能进行查询请求。
分析系统登录部分代码发现,仍然是一个ajax post请求后台的代码,截图如下:
从js代码可以看出res=899为登录失败,其它为登录成功。
于是思路就确定了,先模拟登陆系统,然后使用相同的cookie,再次请求查询即可获得数据。
登录方法:
public static string PostLogin(string postData, string requestUrlString, ref CookieContainer cookie) { UTF8Encoding encoding = new UTF8Encoding(); byte[] data =http://www.mamicode.com/ 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(); }
登录进系统后查询方法:
public static string PostRequest(string postData, string requestUrlString, CookieContainer cookie) { UTF8Encoding encoding = new UTF8Encoding(); byte[] data =http://www.mamicode.com/ 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=http://www.mamicode.com/{/"username\":\"登录账号\",\"userpwd\":\"密码\"}"; string result_login = UtilHelper.PostLogin(postData_login, url_login, ref cc); if (result_login.Equals("1748"))//1748表示登录成功 { string url_getRyData = http://www.mamicode.com/"http://10.77.197.23:7001/yzjy/Rygl.do?method=getRyData"; string postData_RyData = http://www.mamicode.com/"aac002=" + sfz + "&aac003=" + xm + "&pageIndex=0&pageSize=30"; string result_RyData =http://www.mamicode.com/ UtilHelper.PostRequest(postData_RyData, url_getRyData, cc); RyData ry = JsonConvert.DeserializeObject<RyData>(result_RyData); if (ry.total <= 0) { MessageBox.Show("对不起,没有查找到当前人信息。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } }
UtilHelper是自己定义的帮助类。返回的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
C#模拟登录后请求查询
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。