2012-10-04 71 views
9

我會問是否有人向我解釋如何從Delphi應用程序的網頁登錄。我在這裏找到的所有例子都證明對我沒用,否則我做錯了什麼。我厭倦了搜索和無法使用的代碼。從Delphi登錄網站

沒有錯誤信息,我甚至會將頁面代碼放入備忘錄,但似乎它是來自登錄頁面(而不是帳戶[儀表板]頁面)的代碼 - 似乎此代碼無法通過認證,我不知道爲什麼。

有什麼不對這個代碼:

procedure Login; 
var 
HTTP: TIdHTTP; 
Param: TStringList; 
S: String; 
begin 
HTTP := TIdHTTP.Create(nil); 
HTTP.CookieManager := Main_Form.CookieManager; 
Param := TStringList.Create; 
Param.Clear; 
Param.Add('login=example'); 
Param.Add('password=example'); 

try 
HTTP.Get ('http://www.filestrum.com/login.html'); 
HTTP.Post('http://www.filestrum.com/login.html', Param); 
S := HTTP.Get ('http://www.filestrum.com/?op=my_account'); 
Main_Form.Memo2.Lines.Add(S); 
finally 
    HTTP.Free; 
    Param.Free; 
end; 
end; 

或與此版本:

procedure Login; 
var 
HTTP: TIdHTTP; 
S: String; 
begin 
HTTP        := TIdHTTP.Create(nil); 
HTTP.CookieManager    := Main_Form.CookieManager; 
HTTP.Request.BasicAuthentication := True; 
HTTP.Request.Username   := 'example'; 
HTTP.Request.Password   := 'example'; 
HTTP.AllowCookies    := True; 
HTTP.HandleRedirects    := True; 

S := HTTP.Get ('http://www.filestrum.com/?op=my_account'); 
Main_Form.Memo2.Lines.Add(S); 
end; 

使用德爾福XE2,有沒有辦法讓這個代碼運行和登錄。這與XE3演示相同。正如我所說,我真的很累,尋找一些解決方案,浪費時間,沒有任何東西。

請大家,一些幫助在這裏。真的需要它。

+0

您還浪費了2段乞求幫助(你已經張貼問題隱含所做的那樣),而是完全忘了提什麼錯誤。編譯時間,運行時間,特定的錯誤消息? – GolezTrol

+0

您是否嘗試刪除'http://www.filestrum.com//?op = my_account'中的多餘'/'? – TLama

+0

沒有錯誤信息,什麼都沒有。我將頁面代碼放入備忘錄,但似乎沒有通過身份驗證。在Firefox中使用相同的用戶名和密碼。 http://www.filestrum.com/?op=my_account - 我的錯誤,但它仍然是一樣的。無論我嘗試什麼 - 都無法從代碼登錄。 – Cohen

回答

7

嘗試這樣:

function Login: string; 
var 
    IdHTTP: TIdHTTP; 
    Request: TStringList; 
    Response: TMemoryStream; 
begin 
    Result := ''; 
    try 
    Response := TMemoryStream.Create; 
    try 
     Request := TStringList.Create; 
     try 
     Request.Add('op=login'); 
     Request.Add('redirect=http://www.filestrum.com'); 
     Request.Add('login=example'); 
     Request.Add('password=example'); 
     IdHTTP := TIdHTTP.Create; 
     try 
      IdHTTP.AllowCookies := True; 
      IdHTTP.HandleRedirects := True; 
      IdHTTP.Request.ContentType := 'application/x-www-form-urlencoded'; 
      IdHTTP.Post('http://www.filestrum.com/', Request, Response); 
      Result := IdHTTP.Get('http://www.filestrum.com/?op=my_account');  
     finally 
      IdHTTP.Free; 
     end; 
     finally 
     Request.Free; 
     end; 
    finally 
     Response.Free; 
    end; 
    except 
    on E: Exception do 
     ShowMessage(E.Message); 
    end; 
end; 
+0

因此'redirect'不是重定向嗎?聽起來很奇怪... – TLama

+0

@ TLama:POST請求發出302,所以它是在客戶端發出新的GET請求 – whosrdaddy

+0

Yesss ..這一個工程! 夥計,非常感謝你。 – Cohen