標籤:images size get 開發 ret str 瀏覽器 encode 資料
需求是這樣子的,想開發一個外掛程式,能夠抓取別的系統的資料,從而實現資料驗證。
比如這樣一個介面:
使用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 = UtilHelper.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 = 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#類比登入後請求查詢