2010-03-16 24 views
3

我試圖編寫一個程序(C#),它可以登錄並在VBulletin論壇中創建新線程。我試過2種方式:使用C#模擬登錄到VBulletin動作#

1)使用HttpWebRequest:登錄完成。但是,創建新線程失敗。這是郵政編碼:

public static void CreateNewThread(string url,string fId, string title, string message, string tag) 
    { 
     url += "newthread.php?do=postthread"; 

     HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); 
     //string result = ""; 

     string values = "subject=" + title 
         + "&message=" + message 
         + "&tag=" + tag 
         + "&do=postthread" 
         + "&f=" + fId 
         + "&s=" 
         + "" 
         ; 

     req.Method = "POST"; 
     req.ContentType = "application/x-www-form-urlencoded"; 
     req.ContentLength = values.Length; 

     ServicePointManager.Expect100Continue = false; // prevents 417 error 

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

     HttpWebResponse c = (HttpWebResponse)req.GetResponse(); 
    } 

當執行上面的代碼時,沒有任何創建者!

2)使用WebBrowser控件:

webBrowser1.Document.GetElementById("navbar_username").InnerText = "admin"; 
webBrowser1.Document.GetElementById("navbar_password").InnerText = "123"; 

,但我不能沒有提交,因爲沒有名稱/ ID和登錄按鈕是一樣的!請告訴我如何提交沒有表單名稱/ ID和按鈕名稱/ ID的表單?

謝謝!

最佳方面,

回答

0

嘗試模擬後的數據,而不是填寫表單的:

string postData = "username=Kurresmack&password=pw&action=login&url=/"; 
webBrowser1.Navigate("www.sweclockers.com/forum/member.php", "", System.Text.Encoding.UTF8.GetBytes(postData), "Content-Type: application/x-www-form-urlencoded\r\n"); 
+0

嗨, 感謝您的回覆!但是你提供的代碼似乎不起作用。它只是重定向到member.php而且狀態是un-login。我在VBB 3x和4x上測試過。 請檢查它! 最好的方面! – Shinichi 2010-03-16 14:15:57

+0

DId您更改值以符合您的要求?我使用了精確的代碼(用戶名和密碼除外)登錄到sweclockers,它的工作就像一個魅力 – 2010-03-16 14:22:34

+0

這是我的代碼: string postData =「username = Admin&password = 123456&action = login&url = /」; (「localhost/vbb4x/member.php」,「」,Encoding.UTF8.GetBytes(postData),「Content-Type:application/x-www-form-urlencoded \ r \ n」); 如果你看到錯誤,告訴我!謝謝 ! – Shinichi 2010-03-16 15:36:07