需求是這樣子的,想開發一個外掛程序,能夠抓取別的系統的數據,從而實現數據驗證。
比如這樣一個界面:
使用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 = 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 = 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