c# 模拟post登录

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

一、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;
        }

原文地址:https://www.cnblogs.com/tianmochou/p/6599981.html