模拟登陆

 

 

POST登陆

  public static string PostLogin(this string url, ref CookieContainer cookieContainer, string paramData)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            Stream responseStream = null;
            try
            { 

                byte[] byteArray = Encoding.UTF8.GetBytes(paramData); ; //转化

                request = (HttpWebRequest)WebRequest.Create(url);
                request.CookieContainer = cookieContainer;
                request.ContentType = "application/x-www-form-urlencoded";
                request.Referer = new Uri(url).Host;
                request.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, */*";
                request.ContentLength = byteArray.Length;


                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36";
                request.Method = "POST";
                Stream newStream = request.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);
                response = (HttpWebResponse)request.GetResponse();
                Encoding encoding = Encoding.GetEncoding("utf-8");
                StreamReader reader = new StreamReader(response.GetResponseStream(), encoding);
                string str = reader.ReadToEnd();
                cookieContainer = request.CookieContainer;
                reader.Close();

                return str;
            }
            catch (Exception)
            {
                if (responseStream != null)
                    responseStream.Close();
                if (response != null)
                    response.Close();
                return string.Empty;
            }
        }

 


登陆成功之后获取新的HTML有时候因为证书的原因所以要这样访问

 public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        { // 总是接受
            return true;
        }



public static string GetHtml(this string url,ref CookieContainer cookieContainer) { ServicePointManager.DefaultConnectionLimit = 1024; ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);//验证服务器证书回调自动验证 string str = ""; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.CookieContainer = cookieContainer; request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36"; request.ContentType = "application/x-www-form-urlencoded"; request.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, */*"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); request.AllowAutoRedirect = true; request.Method = "GET"; // request.CookieContainer = null; request.Timeout = 60; request.Referer = url; Encoding encoding = Encoding.GetEncoding(response.CharacterSet); try { using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) { str = reader.ReadToEnd(); } } catch { } finally { if (request != null) { request.Abort(); } } return str; }

 Cookie的转换

 

 public static void CookieStr2CookieContainer(this string cookiestr, ref CookieContainer cc, string domain)
        {
            if (cc == null)
                cc = new CookieContainer();

            string[] cookstr = cookiestr.Split(';');
            foreach (string str in cookstr)
            {
                string[] cookieNameValue = str.Split('=');
                if (cookieNameValue.Length > 1)
                {
                    Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
                    ck.Domain = domain;
                    cc.Add(ck);
                }
            }

            

        }
        public static List<Cookie> CookieContainer2CookieStr(CookieContainer cc)
        {
            List<Cookie> lstCookies = new List<Cookie>();

            Hashtable table = (Hashtable)cc.GetType().InvokeMember("m_domainTable",
                System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField |
                System.Reflection.BindingFlags.Instance, null, cc, new object[] { });

            foreach (object pathList in table.Values)
            {
                SortedList lstCookieCol = (SortedList)pathList.GetType().InvokeMember("m_list",
                    System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField
                    | System.Reflection.BindingFlags.Instance, null, pathList, new object[] { });
                foreach (CookieCollection colCookies in lstCookieCol.Values)
                    foreach (Cookie c in colCookies) lstCookies.Add(c);
            }

            return lstCookies;
        }

 

posted @ 2016-12-30 17:20  咸鱼公子  Views(187)  Comments(0Edit  收藏  举报