【转载】.NET模拟POST登录并保持登录状态

2020-11-22 18:31

阅读:719

标签:style   blog   class   code   java   tar   ext   javascript   color   get   int   

好了,还是由于工作需要 要登录一个网站并且模拟点击下载某些东西

原理就是先对一个地址(地址是用户名和密码输入框所在的form的action对应的页面)进行POST提交用户名和密码(不考虑验证码,当然验证码也可以破解),用一个CookieContainer保持Response的cookie,保证了每次请求使用同一个已经登录的session。

本类部分代码来源于网上,功能有待完善,目前只实现了登录和登录后访问“后台”页面。

代码:

mamicode.com,搜素材
mamicode.com,搜素材mamicode.com,搜素材
  public class LogingHelper
    {
        public static CookieContainer theCC = new CookieContainer();
        /// 
        /// 登录方法(无验证码)
        /// 
        /// POST请求的地址
        /// 参数列表 例如 name=zhangsan&pass=lisi
        /// 来源地址
        /// 
        public static string Login(String url, String paramList, string referer)
        {
            HttpWebResponse res = null;
            HttpWebRequest req = null;
            string strResult = "";
            try
            {
                req = (HttpWebRequest)WebRequest.Create(url);
                //配置请求header
                req.Headers.Add(HttpRequestHeader.AcceptCharset, "GBK,utf-8;q=0.7,*;q=0.3");
                req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate,sdch");
                req.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-CN,zh;q=0.8");
                req.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
                req.KeepAlive = true;
                req.Referer = referer;
                req.Headers.Add(HttpRequestHeader.CacheControl, "max-age=0");
                req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.5 Safari/534.7";
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
                req.AllowAutoRedirect = true;
                //设置cookieContainer用来接收cookie
                req.CookieContainer = theCC;
                StringBuilder UrlEncoded = new StringBuilder();
                //对参数进行encode
                Char[] reserved = { ?, =, & };
                byte[] SomeBytes = null;
                if (paramList != null)
                {
                    int i = 0, j;
                    while (i  paramList.Length)
                    {
                        j = paramList.IndexOfAny(reserved, i);
                        if (j == -1)
                        {
                            UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length - i)));
                            break;
                        }
                        UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j - i)));
                        UrlEncoded.Append(paramList.Substring(j, 1));
                        i = j + 1;
                    }
                    SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
                    req.ContentLength = SomeBytes.Length;
                    Stream newStream = req.GetRequestStream();
                    newStream.Write(SomeBytes, 0, SomeBytes.Length);
                    newStream.Close();
                }
                else
                {
                    req.ContentLength = 0;
                }
                //返回请求
                res = (HttpWebResponse)req.GetResponse();
                Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
                Stream responseStream = null;
                if (res.ContentEncoding.ToLower() == "gzip")
                {
                    responseStream = new System.IO.Compression.GZipStream(res.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                }
                else if (res.ContentEncoding.ToLower() == "deflate")
                {
                    responseStream = new System.IO.Compression.DeflateStream(res.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                }
                else
                {
                    responseStream = res.GetResponseStream();
                }
                StreamReader sr = new StreamReader(responseStream, encode);
                strResult = sr.ReadToEnd();
            }
            catch (Exception e)
            {
                //writeLog
            }
            finally
            {
                res.Close();
            }
            return strResult;
        }
        ///   获取页面HTML
        /// 
        /// 
        /// 
        /// 
        public static string getPage(String url, string referer)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            string strResult = string.Empty;
            req.Headers["If-None-Match"] = "36d0ed736e88c71:d9f";
            req.Referer = referer;
            req.CookieContainer = theCC;
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader sr = null;
            try
            {
                sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
                strResult = sr.ReadToEnd();
            }
            catch (System.Exception ex)
            {
                //writeLog
            }
            finally
            {
                sr.Close();
            }
            return strResult;
        }

        /// 
        /// 模仿异步请求POST的方法
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static string VisitPage(string url, string referer, string paramList)
        {
            HttpWebResponse response = null;
            string strResult = string.Empty;
            try
            {
                HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
                request.Method = "POST";
                request.KeepAlive = true;
                request.Referer = referer;
                request.Headers.Add(HttpRequestHeader.AcceptCharset, "GBK,utf-8;q=0.7,*;q=0.3");
                request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate,sdch");
                request.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-CN,zh;q=0.8");
                request.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
                request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.5 Safari/534.7";
                request.ContentType = "application/x-www-form-urlencoded";
                request.CookieContainer = theCC;
                request.Headers.Add("X-Requested-With", "XMLHttpRequest");
                StringBuilder UrlEncoded = new StringBuilder();
                //对参数进行encode
                Char[] reserved = { ?, =, & };
                byte[] SomeBytes = null;
                if (paramList != null)
                {
                    int i = 0, j;
                    while (i  paramList.Length)
                    {
                        j = paramList.IndexOfAny(reserved, i);
                        if (j == -1)
                        {
                            UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length - i)));
                            break;
                        }
                        UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j - i)));
                        UrlEncoded.Append(paramList.Substring(j, 1));
                        i = j + 1;
                    }
                    SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
                    request.ContentLength = SomeBytes.Length;
                    Stream newStream = request.GetRequestStream();
                    newStream.Write(SomeBytes, 0, SomeBytes.Length);
                    newStream.Close();
                }
                response = (HttpWebResponse)request.GetResponse();
                Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
                Stream responseStream = null;
                if (response.ContentEncoding.ToLower() == "gzip")
                {
                    responseStream = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                }
                else if (response.ContentEncoding.ToLower() == "deflate")
                {
                    responseStream = new System.IO.Compression.DeflateStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                }
                else
                {
                    responseStream = response.GetResponseStream();
                }
                StreamReader sr = new StreamReader(responseStream, encode);
                strResult = sr.ReadToEnd();
            }
            catch
            {
                //dosomething
            }
            finally
            {
                response.Close();
            }
            return strResult;
        }
    }
View Code
mamicode.com,搜素材

调用示例:

mamicode.com,搜素材
mamicode.com,搜素材mamicode.com,搜素材
        static void Main(string[] args)
        {
            string userName = "lxnanhr";
            string userPwd = "lxnanhr";
            string content = LogingHelper.Login("http://www.aa.com/index.php?c=login&m=chklogin", "uname=xxx&pass=xxx", "http://www.aa.com/index.php?c=login");
            Console.WriteLine(content);
            Console.WriteLine(LogingHelper.getPage("http://www.aa.com/index.php?c=preview&m=view&resume_id=1179406405&keyword=", "http://www.aa.com/index.php?c=search&m=supersearch#"));
            Console.ReadLine();
        }
View Code
mamicode.com,搜素材

 

【转载】.NET模拟POST登录并保持登录状态,搜素材,soscw.com

【转载】.NET模拟POST登录并保持登录状态

标签:style   blog   class   code   java   tar   ext   javascript   color   get   int   

原文地址:http://www.cnblogs.com/a7265813/p/3702772.html


评论


亲,登录后才可以留言!