Johnny February 2016

Login OK to site with TwebBrowser , but not with TidHTTP

May I ask for a little help using Indy to login to a website please?

Firstly, just as a 'proof of concept' I used a TWebBrowser to test my credentials in the following manner ...

procedure TfrmMain.cxButton1Click(Sender: TObject);
begin
  webBrow.Navigate('http://assurance.redtractor.org.uk/rtassurance/services.eb');
end;

procedure TfrmMain.webBrowDocumentComplete(ASender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  CurrentBrowser: IWebBrowser2;
  TopBrowser: IWebBrowser2;
  Document: OleVariant;
  Doc3 :  IHTMLDocument3;
  Frm  :  IHtmlFormElement;

begin
 CurrentBrowser := pDisp as IWebBrowser2;
 TopBrowser := (ASender as TWebbrowser).DefaultInterface;
 if Assigned(CurrentBrowser) and Assigned(TopBrowser) then
   begin
       if CurrentBrowser = TopBrowser then
       begin
         Doc3 := CurrentBrowser.Document as IHTMLDocument3;
         Webbrow.OnDocumentComplete := nil; // remove handler to avoid reentrance
               Doc3.getElementById('el9M9AQXIL51JI3_loginPnl_username').setAttribute('value', 'aValidUserName', 0);
     Doc3.getElementById('el9M9AQXIL51JI3_loginPnl_password').setAttribute('value', 'aValidPassword', 0);
     //Frm := Doc3.getElementById('ct100') as IHtmlFormElement;
     Doc3.GetElementByID('el9M9AQXIL51JI3_loginPnl_button').click();
    end;
  end;
end;

I got the above from the whosrdaddy answer here Automated Log In (webBrowser)

That logs me into the site and takes me to a search page ... exactly what I need.

However, I'd like to avoid using a TWebBrowser as I thought my searches would be slow due to the fact the page would need to be rendered. With that in mind I tried to use Indy 10 to login to the same address, passing the parameters like so ...

idRedTractor.Post(login_URL, Request, Response);

But all this returns is a 'Server Error,

Answers


whosrdaddy February 2016

Ok, found your problem. The site is doing a redirect to the same page after the POST login request. The key to the solution is setting HandleRedirects to True and change the VMethod variable to GET in the OnHandleRedirect event. I cleaned up the code a bit:

unit SO35263785Test;

interface

uses
  IdHttp,
  SysUtils,
  StrUtils,
  StdCtrls,
  Classes,
  Controls,
  Forms;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    Client : TIdHttp;
    procedure HandleRedirect(Sender: TObject; var dest: string; var NumRedirect: Integer; var Handled: boolean; var VMethod: TIdHTTPMethod);
    procedure LoginToRedTractor(const Username, Password : String);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.HandleRedirect(Sender: TObject; var dest: string; var NumRedirect: Integer; var Handled: boolean; var VMethod: TIdHTTPMethod);
begin
 VMethod := Id_HTTPMethodGet;
 Handled := True;
end;

procedure ExtractViewStateAndGenerator(const Html : String; var ViewState : String; var ViewStateGenerator: String);

var
  Ps : Integer;

begin
 ViewState :=  '';
 ViewStateGenerator := '';
 // we assume __VIEWSTATE and __VIEWSTATEGENERATOR inputs are there, NO error checking
 Ps := Pos('__VIEWSTATE', Html);
 Ps := PosEx('value', Html, Ps);
 Ps := PosEx('"', Html, Ps);
 ViewState := Copy(Html, Ps+1, PosEx('"', Html, Ps+1)-Ps-1);
 Ps := Pos('__VIEWSTATEGENERATOR', Html);
 Ps := PosEx('value', Html, Ps);
 Ps := PosEx('"', Html, Ps);
 ViewStateGenerator := Copy(Html, Ps+1, PosEx('"', Html, Ps+1)-Ps-1);
end;

procedure TForm1.LoginToRedTractor(const Username, Password : String);

var
  GETResponse        : String;
  Request            : TStringList;
  ViewState          : String;
  ViewStateGenerator : String;

begin
 Client := TIdHttp.Create;
 try
  Client.ProtocolVersion : 

Post Status

Asked in February 2016
Viewed 3,843 times
Voted 12
Answered 1 times

Search




Leave an answer