从Delphi登录网站

11
我想请教一下,有没有人能够友好地向我解释如何从Delphi应用程序登录网页。我在这里找到的所有示例都对我无用或者我做错了什么。我已经厌倦了搜索和那些不起作用的代码。
没有错误信息,我甚至将页面代码放入Memo中,但似乎这是登录页面的代码(而不是帐户[仪表板]页面)-似乎这段代码根本不能通过身份验证,我不知道为什么。
这段代码有什么问题:
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;

我使用Delphi XE2,但是无法运行并登录。与XE3演示相同。如我所说,我真的很累了,寻找解决方案已经浪费了几天时间,却一无所获。

请求各位大神,在这里给予一些帮助。我真的非常需要它。


你还浪费了两段时间来请求帮助(通过发布问题已经隐含地这样做了),但完全忘记了提到出了什么问题。编译时错误、运行时错误、特定的错误信息? - GolezTrol
你尝试过删除http://www.filestrum.com//?op=my_account中多余的/吗? - TLama
没有错误信息,什么都没有。我将页面代码放入备忘录中,但似乎没有通过身份验证。相同的用户名和密码从Firefox中可以使用。http://www.filestrum.com/?op=my_account - 这是我的错误,但仍然是一样的。无论我尝试什么 - 都无法从代码登录。 - Cohen
确保您拥有登录POST请求所需的所有参数。我敢打赌你至少缺少了op(操作类型?)。理论上,无法验证(没有Delphi手头),您可以尝试仅进行POST请求并将“重定向”URL指定为您在那个额外斜杠(在问题编辑之前)的位置。 - TLama
我尽了我所知道的一切,但结果仍然是一样的 :( - Cohen
这一定是与cookies有关的问题,不知道还可能是什么。 - Cohen
1个回答

9

可以尝试以下方法:

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;

@TLama:POST 请求发出了 302,所以由客户端发出新的 GET 请求。 - whosrdaddy
@BotenAnna,没有其他的了 :-) - TLama
哈哈,终于成功了。我在这里浪费了好几天的时间,却毫无头绪。TLama 的代码接近有效版本,但最终还是 whosrdaddy 的代码奏效了。这对我来说也很奇怪。谢谢你们,再次感谢。拥抱! - Cohen
1
@TLama:现在我明白你的反应了,你只需要在IdHTTP上包含一个OnRedirect处理程序,所以这个解决方案并不是100%正确的。 - whosrdaddy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接