读取网页刷新过快Cookie丢失,怎么办啊

时间:2022-07-12 16:59:41
   到底是怎么回事啊。 他就这么容易丢失了。。    刷到50次的时候就自动丢失了。   5秒一刷的话就没事。COOKIE不会丢失。。 神啊, 教教我吧。   

HTTPWEBREQUEST 刷的。。  

在线等!!!!!!!!!!

8 个解决方案

#1


该回复于2010-11-13 09:55:13被版主删除

#2



        public void FreeCookie()
        {
            if (myCookieContainer != null)
            {
                myCookieContainer = null;
            }

            myCookieContainer = new CookieContainer();
       //     myCookieContainer.Capacity = 1000000;
           
        }





    public string GetResponse1(string url, string postData, string method, string RequestUri)
        {
            lock (this)
            {
                url.Trim();
                HttpWebRequest request = null;
                try
                {
                    request = (HttpWebRequest)WebRequest.Create(url);
                }
                catch
                {
                    return "";
                }
                if (request == null)
                    return "";
                request.Method = method;
                request.Timeout = 50000;
                request.KeepAlive = false;
                request.AllowAutoRedirect = false;
                request.UseDefaultCredentials = true;
              //  request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
               // request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727; CIBA)";
               // request.Headers.Add("Pragma", "no-cache");
              //  request.Headers.Add("Expires", "-1");
                // WebProxy proxy = new WebProxy();
                
                //  request.Proxy = proxy;
                //   request.UseDefaultCredentials = true;     
                //     request.Headers.Add("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
                //request.Headers.Add("Accept-Language", "zh-cn,zh;q=0.5");
                //

                
                    request.CookieContainer = myCookieContainer;
                
                   // request.Headers.Add("Cookie", m_BaseInfo.strCookies);
                
                
               //   request.ServicePoint.ConnectionLimit = 10000;


                   /* try
                    { }
                    //   if (RequestUri != null && RequestUri != "")
                    //   request.Referer = RequestUri;
                    //   else
                    //   request.Referer = request.RequestUri.ToString();
                    // }
                    catch
                    {
                    }*/

              //  request.ContentType = "application/x-www-form-urlencoded";
               
                HttpWebResponse webresponse = null;

               // webresponse.Cookies = myCookieContainer.GetCookies(request.RequestUri);
                try
                {
                    if (postData.Length > 0)
                    {
                        byte[] loginDataBytes = Encoding.Default.GetBytes(postData);
                        request.ContentLength = loginDataBytes.Length;
                        Stream stream = request.GetRequestStream();
                        stream.Write(loginDataBytes, 0, loginDataBytes.Length);
                        stream.Close();
                    }
                    webresponse = request.GetResponse() as HttpWebResponse;

                 //   if (id == 0)
                  //  {
                   //     string[] strArray = webresponse.Headers.Get("Set-Cookie").Split(new char[] { ';' });
                   //     this.m_BaseInfo.strCookies = strArray[0];
                   // }
                 //   myCookieContainer = request.CookieContainer;
                    id++;
                    if (webresponse != null)
                    {
                      //  return GetResponseHTML1(webresponse);
                      //  webresponse.Close();
                    }
                }
                catch
                {
                    
                    if (null != webresponse)
                    {
                        webresponse.Close();
                    }
                    if (null != request)
                    {
                      //  request.Abort();
                    }
                }
                finally
                {
                    if (null != webresponse)
                    {
                        webresponse.Close();
                    }
                    if (null != request)
                    {
                        //request.c
                    }
                }

                return "";
            }
        }

#3


        public string getJiner()
        {
         string dd=  GetResponse(this.m_BaseInfo.strNewWebAddress +"/leftAllInOneAccount_data.aspx ", "accountUpdate=mini", "post", "");
          jiner1 ="沙巴当前金额:" + getData(dd, "var txt_betcredit= \"", "\";");
          return jiner1;
        }
        public string sbData;
        public string getData()
        {
            try
            {
                sbData = GetResponse(this.m_BaseInfo.strNewWebAddress + "/BetListMini_data.aspx?showBetAcceptedMsg=no", "", "get", "");
                return sbData;
            }
            catch (Exception)
            {
                return "";
            }
        }
        public string info;
        public void getInfo(object d)
        {
            try
            {
                lock (this)
                {
                    
                    string rt = "W";
                    if (!string.IsNullOrEmpty(d.ToString()))
                    {
                        rt = "U";
                    }
                    //GetResponse1(this.m_BaseInfo.strNewWebAddress + "/login_checkin.aspx", "username=" + this.m_BaseInfo.strUserName.ToUpper(), "POST", this.m_BaseInfo.strNewWebAddress + "/topmenu.aspx");
                    info = GetResponse1(this.m_BaseInfo.strNewWebAddress + "/UnderOver_data.aspx?Market=l&Sport=1&DT=&RT=" + rt + "&CT=" + d.ToString() + "&Game=0&OrderBy=0&OddsType=" + pan+"&id="+id, "", "GET","");
                }
            }
            catch (Exception)
            {
                info = "";
            }
        }
        public Image GetWebImage()
        {
            try
            {
                string url = this.m_BaseInfo.strWebAddress+"/login_code.aspx?t=" + System.Environment.TickCount.ToString();
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.AllowAutoRedirect = true;
                req.Method = "GET";

             //   req.Headers.Add("Accept-Language", "zh-cn");
                req.Headers.Add("Accept-Encoding", "gzip,deflate");
             //   req.Headers.Add("P3P", "CP=CAO PSA OUR");
                //req.Headers.Add("Cookie", "LangKey=cs;");
                req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727; CIBA)";
                req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                req.CookieContainer = myCookieContainer;// new CookieContainer();
                req.KeepAlive = true;
                req.Timeout = 5000;
                HttpWebResponse webresponse = null;
                try
                {
                    webresponse = (HttpWebResponse)req.GetResponse();
                    if (webresponse != null)
                    {
                        return Image.FromStream(webresponse.GetResponseStream());
                    }
                }
                catch
                {
                    return null;
                }
                finally
                {
                    if (webresponse != null)
                    {
                        webresponse.Close();
                    }
                    if (req != null)
                    {
                        req.Abort();
                    }
                }

                return null;
            }
            catch
            {
                return null;
            }
        }

#4


帮忙看下吧。!

#5


什么情况?

#6


我觉得应该是网站服务器有限制的问题,首先session是不会超时的,50次之前都可以抓取到,从50之后就没了,这多半都是和服务器有关了。

你可以在50次的时候,将程序暂停一段时间后再抓取,或者adsl断网一下,看看能不能解决。

#7


暂停5秒没事,可能这个5秒钟满足了你抓取的那个网站限制的时间,所以50次后还可以。猜测的。

#8


哎!!!!!!真郁闷。

#1


该回复于2010-11-13 09:55:13被版主删除

#2



        public void FreeCookie()
        {
            if (myCookieContainer != null)
            {
                myCookieContainer = null;
            }

            myCookieContainer = new CookieContainer();
       //     myCookieContainer.Capacity = 1000000;
           
        }





    public string GetResponse1(string url, string postData, string method, string RequestUri)
        {
            lock (this)
            {
                url.Trim();
                HttpWebRequest request = null;
                try
                {
                    request = (HttpWebRequest)WebRequest.Create(url);
                }
                catch
                {
                    return "";
                }
                if (request == null)
                    return "";
                request.Method = method;
                request.Timeout = 50000;
                request.KeepAlive = false;
                request.AllowAutoRedirect = false;
                request.UseDefaultCredentials = true;
              //  request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
               // request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727; CIBA)";
               // request.Headers.Add("Pragma", "no-cache");
              //  request.Headers.Add("Expires", "-1");
                // WebProxy proxy = new WebProxy();
                
                //  request.Proxy = proxy;
                //   request.UseDefaultCredentials = true;     
                //     request.Headers.Add("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
                //request.Headers.Add("Accept-Language", "zh-cn,zh;q=0.5");
                //

                
                    request.CookieContainer = myCookieContainer;
                
                   // request.Headers.Add("Cookie", m_BaseInfo.strCookies);
                
                
               //   request.ServicePoint.ConnectionLimit = 10000;


                   /* try
                    { }
                    //   if (RequestUri != null && RequestUri != "")
                    //   request.Referer = RequestUri;
                    //   else
                    //   request.Referer = request.RequestUri.ToString();
                    // }
                    catch
                    {
                    }*/

              //  request.ContentType = "application/x-www-form-urlencoded";
               
                HttpWebResponse webresponse = null;

               // webresponse.Cookies = myCookieContainer.GetCookies(request.RequestUri);
                try
                {
                    if (postData.Length > 0)
                    {
                        byte[] loginDataBytes = Encoding.Default.GetBytes(postData);
                        request.ContentLength = loginDataBytes.Length;
                        Stream stream = request.GetRequestStream();
                        stream.Write(loginDataBytes, 0, loginDataBytes.Length);
                        stream.Close();
                    }
                    webresponse = request.GetResponse() as HttpWebResponse;

                 //   if (id == 0)
                  //  {
                   //     string[] strArray = webresponse.Headers.Get("Set-Cookie").Split(new char[] { ';' });
                   //     this.m_BaseInfo.strCookies = strArray[0];
                   // }
                 //   myCookieContainer = request.CookieContainer;
                    id++;
                    if (webresponse != null)
                    {
                      //  return GetResponseHTML1(webresponse);
                      //  webresponse.Close();
                    }
                }
                catch
                {
                    
                    if (null != webresponse)
                    {
                        webresponse.Close();
                    }
                    if (null != request)
                    {
                      //  request.Abort();
                    }
                }
                finally
                {
                    if (null != webresponse)
                    {
                        webresponse.Close();
                    }
                    if (null != request)
                    {
                        //request.c
                    }
                }

                return "";
            }
        }

#3


        public string getJiner()
        {
         string dd=  GetResponse(this.m_BaseInfo.strNewWebAddress +"/leftAllInOneAccount_data.aspx ", "accountUpdate=mini", "post", "");
          jiner1 ="沙巴当前金额:" + getData(dd, "var txt_betcredit= \"", "\";");
          return jiner1;
        }
        public string sbData;
        public string getData()
        {
            try
            {
                sbData = GetResponse(this.m_BaseInfo.strNewWebAddress + "/BetListMini_data.aspx?showBetAcceptedMsg=no", "", "get", "");
                return sbData;
            }
            catch (Exception)
            {
                return "";
            }
        }
        public string info;
        public void getInfo(object d)
        {
            try
            {
                lock (this)
                {
                    
                    string rt = "W";
                    if (!string.IsNullOrEmpty(d.ToString()))
                    {
                        rt = "U";
                    }
                    //GetResponse1(this.m_BaseInfo.strNewWebAddress + "/login_checkin.aspx", "username=" + this.m_BaseInfo.strUserName.ToUpper(), "POST", this.m_BaseInfo.strNewWebAddress + "/topmenu.aspx");
                    info = GetResponse1(this.m_BaseInfo.strNewWebAddress + "/UnderOver_data.aspx?Market=l&Sport=1&DT=&RT=" + rt + "&CT=" + d.ToString() + "&Game=0&OrderBy=0&OddsType=" + pan+"&id="+id, "", "GET","");
                }
            }
            catch (Exception)
            {
                info = "";
            }
        }
        public Image GetWebImage()
        {
            try
            {
                string url = this.m_BaseInfo.strWebAddress+"/login_code.aspx?t=" + System.Environment.TickCount.ToString();
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.AllowAutoRedirect = true;
                req.Method = "GET";

             //   req.Headers.Add("Accept-Language", "zh-cn");
                req.Headers.Add("Accept-Encoding", "gzip,deflate");
             //   req.Headers.Add("P3P", "CP=CAO PSA OUR");
                //req.Headers.Add("Cookie", "LangKey=cs;");
                req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727; CIBA)";
                req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                req.CookieContainer = myCookieContainer;// new CookieContainer();
                req.KeepAlive = true;
                req.Timeout = 5000;
                HttpWebResponse webresponse = null;
                try
                {
                    webresponse = (HttpWebResponse)req.GetResponse();
                    if (webresponse != null)
                    {
                        return Image.FromStream(webresponse.GetResponseStream());
                    }
                }
                catch
                {
                    return null;
                }
                finally
                {
                    if (webresponse != null)
                    {
                        webresponse.Close();
                    }
                    if (req != null)
                    {
                        req.Abort();
                    }
                }

                return null;
            }
            catch
            {
                return null;
            }
        }

#4


帮忙看下吧。!

#5


什么情况?

#6


我觉得应该是网站服务器有限制的问题,首先session是不会超时的,50次之前都可以抓取到,从50之后就没了,这多半都是和服务器有关了。

你可以在50次的时候,将程序暂停一段时间后再抓取,或者adsl断网一下,看看能不能解决。

#7


暂停5秒没事,可能这个5秒钟满足了你抓取的那个网站限制的时间,所以50次后还可以。猜测的。

#8


哎!!!!!!真郁闷。