使用C#的HttpWebRequest模拟登陆网站

这篇文章是有关模拟登录网站方面的。

实现步骤;

  1. 启用一个web会话
  2. 发送模拟数据请求(POST或者GET)
  3. 获取会话的CooKie 并根据该CooKie继续访问登录后的页面,获取后续访问的页面数据。

我们以登录人人网为例,首先需要分析人人网登录时POST的数据格式,这个可以通过IE9中只带的F12快捷键,调出开发人员工具。如下图:

 

通过开始捕获得到POST的地址和POST的数据

POST数据:

email=aaa@163.com&password=111&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&_rtk=90484476

POST地址:

http://www.renren.com/PLogin.do

下面就是代码示例来得到登录后页面(http://guide.renren.com/guide)的数据

HTMLHelper类

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using System.Threading; namespace Test {    public class HTMLHelper     {        /// <summary>         /// 获取CooKie        /// </summary>        /// <param name="loginUrl"></param>        /// <param name="postdata"></param>        /// <param name="header"></param>        /// <returns></returns>       public static CookieContainer GetCooKie(string loginUrl, string postdata, HttpHeader header)        {            HttpWebRequest request = null;            HttpWebResponse response = null;            try            {                CookieContainer cc = new CookieContainer();                request = (HttpWebRequest)WebRequest.Create(loginUrl);                request.Method = header.method;                request.ContentType = header.contentType;                byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);                request.ContentLength = postdatabyte.Length;                request.AllowAutoRedirect = false;                request.CookieContainer = cc;                request.KeepAlive = true;
               //提交请求               Stream stream;                stream = request.GetRequestStream();                stream.Write(postdatabyte, 0, postdatabyte.Length);                stream.Close();
               //接收响应               response = (HttpWebResponse)request.GetResponse();                response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);                CookieCollection cook = response.Cookies;                //Cookie字符串格式               string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);                return cc;            }            catch (Exception ex)            {                throw ex;            }        }        /// <summary>        /// 获取html        /// </summary>        /// <param name="getUrl"></param>        /// <param name="cookieContainer"></param>        /// <param name="header"></param>        /// <returns></returns>       public static string GetHtml(string getUrl, CookieContainer cookieContainer,HttpHeader header)        {            Thread.Sleep(1000);            HttpWebRequest httpWebRequest = null;            HttpWebResponse httpWebResponse = null;            try            {                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl);                httpWebRequest.CookieContainer = cookieContainer;                httpWebRequest.ContentType = header.contentType;                httpWebRequest.ServicePoint.ConnectionLimit = header.maxTry;                httpWebRequest.Referer = getUrl;                httpWebRequest.Accept = header.accept;                httpWebRequest.UserAgent = header.userAgent;                httpWebRequest.Method = "GET";                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();                Stream responseStream = httpWebResponse.GetResponseStream();                StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);                string html = streamReader.ReadToEnd();                streamReader.Close();                responseStream.Close();                httpWebRequest.Abort();                httpWebResponse.Close();                return html;            }            catch (Exception e)            {                if (httpWebRequest != null) httpWebRequest.Abort();                if (httpWebResponse != null) httpWebResponse.Close();                return string.Empty;            }        }     }
   public class HttpHeader    {        public string contentType { getset; }        public string accept { getset; }        public string userAgent { getset; }        public string method{get;set;}        public int maxTry { getset; }    } }

 

测试用例:

 
HttpHeader header = new HttpHeader();               
 header.accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
                header.contentType = "application/x-www-form-urlencoded";
                header.method = "POST";
                header.userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
                header.maxTry = 300;

                string html = HTMLHelper.GetHtml("http://guide.renren.com/guide", HTMLHelper.GetCooKie("http://www.renren.com/PLogin.do",
                    "email=aaa@163.com&password=111&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&_rtk=90484476", header), header);

                Console.WriteLine(html);


               Console.ReadLine();

 

通过程序登录了网站后而直接进入登录后的页面。

首先还是发起一个启用一个web会话,然后发送模拟数据请求,获取会话的CooKie,再根据该CooKie将其写入到本地,通过程序直接打开登录后的页面。

该功能可用于无法修改第三方系统源代码而要做系统单点登录。

 

我们先在HTMLHelper类中添加一个方法:

    /// <summary>     /// 获取CookieCollection     /// </summary>     /// <param name="loginUrl"></param>     /// <param name="postdata"></param>     /// <param name="header"></param>     /// <returns></returns>    public static CookieCollection GetCookieCollection(string loginUrl, string postdata, HttpHeader header)     {         HttpWebRequest request = null;         HttpWebResponse response = null;         try         {             CookieContainer cc = new CookieContainer();             request = (HttpWebRequest)WebRequest.Create(loginUrl); request.Method = header.method;             request.ContentType = header.contentType;             byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);             request.ContentLength = postdatabyte.Length;             request.AllowAutoRedirect = false;             request.CookieContainer = cc;             request.KeepAlive = true;
            //提交请求            Stream stream;             stream = request.GetRequestStream();             stream.Write(postdatabyte, 0, postdatabyte.Length);             stream.Close();
            //接收响应            response = (HttpWebResponse)request.GetResponse();             response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);             CookieCollection cook = response.Cookies;             //Cookie字符串格式            string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);             return cook;         }         catch (Exception ex)         {             throw ex;         }     }


再根据获取的CookieCollection写入本地,并打开登录后的页面

    [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]     public bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData)     {         HttpHeader header = new HttpHeader();         header.accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";         header.contentType = "application/x-www-form-urlencoded";         header.method = "POST";         header.userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";         header.maxTry = 300;         CookieCollection mycookie = HTMLHelper.GetCookieCollection("http://www.renren.com/PLogin.do",                            "email=aaa%40163.com&password=111&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&_rtk=90484476", header);         foreach (Cookie cookie in mycookie) //将cookie设置为浏览的cookie          {             InternetSetCookie("http://" + cookie.Domain.ToString(), cookie.Name.ToString(), cookie.Value.ToString() + ";expires=Sun,22-Feb-2099 00:00:00 GMT");         }         System.Diagnostics.Process.Start("http://guide.renren.com/guide");     }

这样即可直接通过程序打开登录后的页面:

 

原文地址:https://www.cnblogs.com/wolfocme110/p/3969542.html