C#模擬登陸後請求查詢

需求是這樣子的,想開發一個外掛程序,可以抓取別的系統的數據,從而實現數據驗證。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

相關文章
相關標籤/搜索