2011-12-28 82 views
1

我創建了一個連接到我的phpBB論壇並登錄的C#應用​​程序。我現在試圖使用webclient轉到頁面並以頁面形式抓取頁面。但是我一直註銷。如何使用用於在webclient上登錄論壇的代碼創建的cookie?保持登錄論壇

代碼用於登錄到論壇,並得到頁:

 public static CookieContainer login(string url, string username, string password, Form1 form) 
     { 
      if (url.Length == 0 || username.Length == 0 || password.Length == 0) 
      { 
       Console.WriteLine("Information missing"); 
       return null; 
      } 

      CookieContainer myContainer = new CookieContainer(); 

      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
      request.CookieContainer = myContainer; 

      // Set type to POST 
      request.Method = "POST"; 
      request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)"; 
      request.ContentType = "application/x-www-form-urlencoded"; 

      // Build the new header, this isn't a multipart/form, so it's very simple 
      StringBuilder data = new StringBuilder(); 
      data.Append("username=" + Uri.EscapeDataString(username)); 
      data.Append("&password=" + Uri.EscapeDataString(password)); 
      data.Append("&login=Login"); 

      // Create a byte array of the data we want to send 
      byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); 

      // Set the content length in the request headers 
      request.ContentLength = byteData.Length; 

      Stream postStream; 
      try 
      { 
       postStream = request.GetRequestStream(); 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("Login - " + e.Message.ToString() + " (GRS)"); 
       return null; 
      } 

      // Write data 
      postStream.Write(byteData, 0, byteData.Length); 

      HttpWebResponse response; 
      try 
      { 
       response = (HttpWebResponse)request.GetResponse(); 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("Login - " + e.Message.ToString() + " (GR)"); 
       return null; 
      } 

      bool isLoggedIn = false; 

      // Store the cookies 
      foreach (Cookie c in response.Cookies) 
      { 
       if (c.Name.Contains("_u")) 
       { 
        if (Convert.ToInt32(c.Value) > 1) 
        { 
         isLoggedIn = true; 

        } 
       } 
       myContainer.Add(c); 
      } 

      if (isLoggedIn) 
      { 

       string _url = "http://www.dandrews.net/forum/custom.php"; 
       string strResult = ""; 

       HttpWebRequest _request = (HttpWebRequest)HttpWebRequest.Create(_url); 
       _request.CookieContainer = myContainer; 
       HttpWebResponse _response = (HttpWebResponse)_request.GetResponse(); 



       using (StreamReader sr = new StreamReader(_response.GetResponseStream())) 
       { 
        strResult = sr.ReadToEnd(); 
        // Close and clean up the StreamReader 
        sr.Close(); 
       } 
       form.userbox.Text = strResult; 

       return myContainer; 

      } 
      else 
      { 
       return null; 
      } 
     } 

回答

1

您使用CookieContainer不同於設計。

CookieContainer myContainer = new CookieContainer(); 

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
request.CookieContainer = new CookieContainer(); 

應該是:

CookieContainer myContainer = new CookieContainer(); 

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
request.CookieContainer = myContainer; 

然後你就可以完全刪除你的邏輯給自己填充容器,因爲該框架會爲你做的。然後,只要確保在第二個請求中使用myContainer實例,它應該可以工作。

更新

如果你必須使用WebClient類你的第二個要求,你可能想看看this question,以幫助您使用CookieContainerWebClient請求。

更新

基於更新後的代碼:

 public static CookieContainer login(string url, string username, string password) 
    { 
     if (url.Length == 0 || username.Length == 0 || password.Length == 0) 
     { 
      Console.WriteLine("Information missing"); 
      return null; 
     } 

     CookieContainer myContainer = new CookieContainer(); 

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
     request.CookieContainer = myContainer; 

     // Set type to POST 
     request.Method = "POST"; 
     request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)"; 
     request.ContentType = "application/x-www-form-urlencoded"; 

     // Build the new header, this isn't a multipart/form, so it's very simple 
     StringBuilder data = new StringBuilder(); 
     data.Append("username=" + Uri.EscapeDataString(username)); 
     data.Append("&password=" + Uri.EscapeDataString(password)); 
     data.Append("&login=Login"); 

     // Create a byte array of the data we want to send 
     byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); 

     // Set the content length in the request headers 
     request.ContentLength = byteData.Length; 

     Stream postStream; 
     try 
     { 
      postStream = request.GetRequestStream(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Login - " + e.Message.ToString() + " (GRS)"); 
      return null; 
     } 

     // Write data 
     postStream.Write(byteData, 0, byteData.Length); 

     HttpWebResponse response; 
     try 
     { 
      response = (HttpWebResponse)request.GetResponse(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine("Login - " + e.Message.ToString() + " (GR)"); 
      return null; 
     } 
     string _url = "http://www.dandrews.net/forum/custom.php"; 

     // Store the cookies 
     if (myContainer.GetCookies(new Uri(url)).Cast<Cookie>().Any(c => c.Name.Contains("_u"))) 
     { 
      string strResult = ""; 

      HttpWebRequest _request = (HttpWebRequest)HttpWebRequest.Create(_url); 
      _request.CookieContainer = myContainer; 
      HttpWebResponse _response = (HttpWebResponse)_request.GetResponse(); 

      using (StreamReader sr = new StreamReader(_response.GetResponseStream())) 
      { 
       strResult = sr.ReadToEnd(); 
       // Close and clean up the StreamReader 
       sr.Close(); 
      } 
      Console.WriteLine(strResult); 

      return myContainer; 
     } 
     else 
     { 
      return null; 
     } 
    }