2012-10-04 69 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