2012-07-18 85 views
0

我想在C#中使用webrequest登錄到xenForo論壇,但我似乎無法讓它正常工作,所以任何幫助將不勝感激。C#登錄xenForo論壇使用Webrequests?

我用小提琴手得到,當我登錄,這是原始POST數據我所發送的POST數據......

POST http://www.----------.com/login/login HTTP/1.1 
Host: www.----------.com 
Connection: keep-alive 
Content-Length: 109 
Cache-Control: max-age=0 
Origin: http://www.----------.com 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11 
Content-Type: application/x-www-form-urlencoded 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Referer: http://www.----------.com/login/login 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: en-US,en;q=0.8 
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Cookie: xf_session=0c4e132b44ce81bdf93e70c57fe17eb6; __cfduid=d43498195638b2afe52ebaa9e1f97b8b31342586809; __cfduid=d43498195638b2afe52ebaa9e1f97b8b31342586809 

login=USERNAME&register=0&password=PASSWORD&remember=1&cookie_check=1&redirect=forum%2F&_xfToken= 

所以之後,我就在嘗試創建的WebRequest複製這一點,這就是我目前有...

private void button1_Click(object sender, EventArgs e) 
     { 
     try 
     { 
     HttpWebRequest http = WebRequest.Create("http://www.----------.com/login/login") as HttpWebRequest; 
     http.KeepAlive = true; 
     http.Method = "POST"; 
     http.AllowAutoRedirect = true; 
     http.ContentType = "application/x-www-form-urlencoded"; 
     string postData="login=" + usernameBox.Text + "&register=0&password=" + passwordBox.Text + "&remember=1&cookie_check=1&redirect=forum%2F&_xfToken="; 
     byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData); 
     http.ContentLength = dataBytes.Length; 
     using (Stream postStream = http.GetRequestStream()) 
     { 
     postStream.Write(dataBytes, 0, dataBytes.Length); 
     } 
     HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse; 
     int y = (int)httpResponse.StatusCode; 
     MessageBox.Show(Convert.ToString(y), "Response Code Debug"); 
     foreach(Cookie c in httpResponse.Cookies) 
     { 
     MessageBox.Show(c.Name + " = " + c.Value, "Cookie Debug"); 
     } 
     http = WebRequest.Create("http://www.----------.com/forum") as HttpWebRequest; 
     http.CookieContainer = new CookieContainer(); 
     http.CookieContainer.Add(httpResponse.Cookies); 
     http.AllowAutoRedirect=false; 
     HttpWebResponse httpResponse2 = http.GetResponse() as HttpWebResponse; 
     } 
     catch (Exception ex) 
     { 
     MessageBox.Show(ex.Message, "Catch Debug"); 
     try 
     { 
     Clipboard.SetText(ex.Message); 
     } 
     catch 
     { 
     } 
     } 

只是忽略了一些,我是用消息框,試圖弄清楚什麼是更精確回事請求,但它並沒有真的非常不幸的幫助。這是我第一次使用webrequests,所以我很抱歉,如果它只是某種愚蠢的錯誤。

我只需要能夠找出登錄是否成功。此外,如果您需要使用xenForo網站,只需使用http://www.shadygamer.com這是我正在嘗試使用的網站。

正如我前面所說,任何和所有的幫助表示讚賞。謝謝。 :)

回答

1

你要送這樣的:

cookie_check=0 

不是這個:

cookie_check=1 

Xenforo CMS不會檢查餅乾。它應該幫助:)。