2010-03-03 55 views
7

我正在嘗試登錄到vbulletin論壇。我能走到今天:如何使用C#登錄vbulletin論壇?

private string login(string url, string username, string password) 
{ 
string values = "vb_login_username={0}&vb_login_password={1}" 
values += "&securitytoken=guest&cookieuser=checked&do=login"; 

values = string.Format(values, username, password); 
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); 
CookieContainer a = new CookieContainer(); 
req.CookieContainer = a; 

System.Net.ServicePointManager.Expect100Continue = false; // prevents 417 error 

using (StreamWriter writer = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII)) 
{ writer.Write(values); } 

this.response = (HttpWebResponse)req.GetResponse(); 

StringBuilder output = new StringBuilder(); 

foreach (var cookie in response.Cookies) 
{ 
output.Append(cookie.ToString()); 
output.Append(";"); 
} 


return output.ToString(); 
} 

它看起來像我正在登錄,但是當我下載的頁面,我無法找到我的用戶名在其中。

你們看到我可能做錯了什麼嗎?

在此先感謝!

回答

1

我不確定我是否關注你。你對VBB意味着什麼?

如果你的意思是在vb.com上發佈一個主題,我可以告訴你有一個線程在vb.org在論壇的'vb3編程討論'(不是由我發佈)中打開。它應該在第3頁上,以'C#'開始。

如果你的意思是你的文章的其他內容,你能澄清一下嗎?

+1

做,如果你想回復的答案,你應該使用「添加評論」鏈接,評論的下方,而不是張貼回答你自己的評論。 – 2010-07-21 20:03:01

6

可以使用HTTP請求

使用此方法

public string Login(Uri ActionUrl, string postData) 
     { 

      gRequest = (HttpWebRequest)WebRequest.Create(formActionUrl); 
      gRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.1.8) Gecko/20100202 Firefox/3.5.8 GTBDFff GTB7.0"; 

      gRequest.CookieContainer = new CookieContainer(); 
      gRequest.Method = "POST"; 
      gRequest.Accept = " text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8, */*"; 
      gRequest.KeepAlive = true; 
      gRequest.ContentType = @"application/x-www-form-urlencoded"; 


      #region CookieManagement 
      if (this.gCookies != null && this.gCookies.Count > 0) 
      { 

       gRequest.CookieContainer.Add(gCookies); 
      } 


      try 
      { 

       string postdata = string.Format(postData); 
       byte[] postBuffer = System.Text.Encoding.GetEncoding(1252).GetBytes(postData); 
       gRequest.ContentLength = postBuffer.Length; 
       Stream postDataStream = gRequest.GetRequestStream(); 
       postDataStream.Write(postBuffer, 0, postBuffer.Length); 
       postDataStream.Close(); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 

      } 



      try 
      { 
       gResponse = (HttpWebResponse)gRequest.GetResponse(); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex); 

      } 



      //check if the status code is http 200 or http ok 

       if (gResponse.StatusCode == HttpStatusCode.OK) 
       { 
        //get all the cookies from the current request and add them to the response object cookies 

        gResponse.Cookies = gRequest.CookieContainer.GetCookies(gRequest.RequestUri); 
        //check if response object has any cookies or not 
        if (gResponse.Cookies.Count > 0) 
        { 
         //check if this is the first request/response, if this is the response of first request gCookies 
         //will be null 
         if (this.gCookies == null) 
         { 
          gCookies = gResponse.Cookies; 
         } 
         else 
         { 
          foreach (Cookie oRespCookie in gResponse.Cookies) 
          { 
           bool bMatch = false; 
           foreach (Cookie oReqCookie in this.gCookies) 
           { 
            if (oReqCookie.Name == oRespCookie.Name) 
            { 
             oReqCookie.Value = oRespCookie.Name; 
             bMatch = true; 
             break; // 
            } 
           } 
           if (!bMatch) 
            this.gCookies.Add(oRespCookie); 
          } 
         } 
        } 
      #endregion 



        StreamReader reader = new StreamReader(gResponse.GetResponseStream()); 
        string responseString = reader.ReadToEnd(); 
        reader.Close(); 
        //Console.Write("Response String:" + responseString); 
        return responseString; 
       } 
       else 
       { 
        return "Error in posting data"; 
       } 

     } 
+0

你可以通過閱讀http標題獲得postData – 2010-09-16 07:28:45

+1

你在哪裏設置用戶名和密碼? – Yustme 2010-09-17 16:34:06