• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

c#模拟post登录

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

使用模拟登录大致可以分为两步

一、post登录获取cookis


        public CookieContainer GetCookie(string url,string account,string password, out bool result)
        {
            CookieContainer cc = new CookieContainer();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            // Set the Method property of the request to POST.  
            request.Method = "POST";
            // Create POST data and convert it to a byte array.  
            //string postData = "";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.  
            //request.ContentType = "application/x-www-form-urlencoded";
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
            request.ProtocolVersion = HttpVersion.Version11;
            request.AllowAutoRedirect = true;
            // Set the ContentLength property of the WebRequest.  
            request.ContentLength = byteArray.Length;
            request.CookieContainer = cc;
            // Get the request stream.  
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.  
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.  
            dataStream.Close();
            // Get the response.  
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            // Display the status.  
            //Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.  
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.  
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.  
            string responseFromServer = reader.ReadToEnd();
            reader.Close();
            dataStream.Close();
            response.Close();
            //判断登录是否成功
            if (responseFromServer.Contains("您填写的账号或密码不正确,请再次尝试"))
                result = false;
            else
                result = true;
            return cc;
        }

其中postdata可以通过网页工具如firebug获取到。找到其中关键处key value如用户名密码等

二、根据获取到的cookie获取网页内容或提交命令

1、根据cookis获取数据

        public string GetHtmlDatas(string url, CookieContainer cc)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.CookieContainer = cc;
            webRequest.Method = "GET";
            //Get the response from the server and save the cookies from the first request..
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            // Get the stream containing content returned by the server.  
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.  
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.  
            string responseFromServer = reader.ReadToEnd();
            return responseFromServer;
        }

2、根据登录成功的cookie操作post命令

        public string PostCommand(string url,CookieContainer cc)
        {
            //http://xs.bgy.com.cn/Sale/RoomQuery/QureyRoomShowDetail.aspx?roomid=LHY038D-1-3101
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            // Set the Method property of the request to POST.  
            request.Method = "POST";
            // Create POST data and convert it to a byte array.  
            string postData = ""
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.  
            //request.ContentType = "application/x-www-form-urlencoded";
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
            request.ProtocolVersion = HttpVersion.Version11;
            request.AllowAutoRedirect = true;
            // Set the ContentLength property of the WebRequest.  
            request.ContentLength = byteArray.Length;
            request.CookieContainer = cc;
            // Get the request stream.  
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.  
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.  
            dataStream.Close();
            // Get the response.  
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            // Display the status.  
            //Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.  
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.  
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.  
            string responseFromServer = reader.ReadToEnd();
            reader.Close();
            dataStream.Close();
            response.Close();
            return responseFromServer;
        }


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#遍历窗体所有控件或某类型所有控件发布时间:2022-07-10
下一篇:
C#bool?的意思发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap