11

DelphiアプリからWebページにログインする方法を誰かが親切に説明してくれるかどうか尋ねます. ここで見つけたすべての例は、役に立たないことが判明したか、何か間違ったことをしています。検索と機能しないコードにうんざりしています。

エラー メッセージは表示されません。ページ コードをメモに入力することもできますが、ログイン ページ (アカウント [ダッシュボード] ページではなく) からのコードのようです。

このコードの何が問題なのですか:

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のデモと同じです。私が言ったように、私はいくつかの解決策を探すのに本当に疲れていて、何日も無駄にしています。

皆さん、ここで助けてください。本当に必要です。

4

1 に答える 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;
于 2012-10-04T09:05:22.637 に答える