zoukankan      html  css  js  c++  java
  • 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 = 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

  • 相关阅读:
    痞子衡嵌入式:利用i.MXRT1060,1010上新增的FlexSPI地址重映射(Remap)功能可安全OTA
    “既生 ExecutorService, 何生 CompletionService?”
    55
    .map() is not a function【js报错】
    内网穿透之流量代理转发
    JDK8 String类知识总结
    Java并发编程(07):Fork/Join框架机制详解
    数据源管理 | 分布式NoSQL系统,Cassandra集群管理
    Solon详解(三)- Solon的web开发
    Solon详解(二)- Solon的核心
  • 原文地址:https://www.cnblogs.com/guwei4037/p/6835294.html
Copyright © 2011-2022 走看看