2012-02-01 21 views
2

我試圖登錄到使用以下網站的HttpWebRequest:http://mostanmeldung.moessinger.at/login.phpHttpWebRequest的(崗位)和重定向

文本是在德國,但他們並不真正的問題。如果你看一下源代碼(順便說一下,我不是這樣寫的,所以不要責怪我的不好的風格:P),你會看到一個包含兩個輸入標籤的表單標籤。第一個名字是「BN」(用戶名),第二個名字是「PW」(密碼)。我正嘗試使用HttpWebRequest類將包含這兩個輸入值的數據發送到Web服務器。但是,發佈值會將請求重定向到另一個名爲「einloggen.php」的頁面。在該網站上,我被告知我的登錄是否成功。

我的問題是我能夠發送數據沒有任何問題,但是,我收到的只是「login.php」的內容,您必須在該網站上輸入用戶名和密碼。

這是我的代碼如下所示:

string post = String.Format(PostPattern, Username, Password); 
byte[] postBytes = Encoding.ASCII.GetBytes(post); 

CookieContainer cookies = new CookieContainer(); 

// "Address": http://mostanmeldung.moessinger.at/login.php 

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Address); 
req.CookieContainer = cookies; 
req.Method = "POST"; 
req.ContentType = "application/x-www-form-urlencoded"; 
req.ContentLength = postBytes.Length; 
req.AllowAutoRedirect = true; 

MessageBox.Show(post); // shows me "BN=boop;PW=hi" 

Stream reqStream = req.GetRequestStream(); 
reqStream.Write(postBytes, 0, postBytes.Length); 
reqStream.Close(); 

WebResponse res; 
if (/*req.HaveResponse &&*/ (res = req.GetResponse()) != null) 
{ 
    StreamReader reader = new StreamReader(res.GetResponseStream()); 
    MessageBox.Show(reader.ReadToEnd()); 
    return AuthResult.Success; 
} 

return AuthResult.NoResponse; 

在22線(5號線年底前)的消息框顯示我的「的login.php」的內容,而不是「einloggen.php」其中我被重定向到。這是爲什麼?

回答

3

該窗體上的操作指向einloggen.php,而不是login.php,因此您需要將POST數據發送到einloggen.php。

+0

哦,是這樣嗎?不知道!謝謝。 – haiyyu 2012-02-01 19:34:57