0

I'm trying to create application that support session using idhttp 10.5.7 on Delphi XE, but the session always changed every time i call the request.

But then I use the same code in Delphi 2006 using indy 10.1.5 the session maintained successfully, even when i call the request multiple times it only create 1 session.

How do I solve this problem in Delphi XE?

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdCookieManager, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdHTTP, StdCtrls,iduri,IdCookie;

type
  TForm1 = class(TForm)
    HTTP: TIdHTTP;
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    function get_dataweb(url: string): string;
    procedure OnNewCookie(ASender: TObject;  ACookie: TIdCookieRFC2109; var VAccept: Boolean);
    { Private declarations }
  public
       Stream : TStringStream;
       cookie : TIdCookieManager;
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var hasil : string;
begin
  hasil :=  get_dataweb('http://localhost/web/tes.php');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Stream := TStringStream.Create;
  cookie := TIdCookieManager.Create;
  cookie.OnNewCookie := OnNewCookie;
  HTTP.CookieManager := cookie;

end;

procedure TForm1.OnNewCookie(ASender: TObject;  ACookie: TIdCookieRFC2109; var VAccept: Boolean);
begin
  memo1.Lines.Add(ACookie.CookieText);
end;

function TForm1.get_dataweb(url:string):string;
var hasil : string;
begin

  stream.Position       := 0;
  stream.Size           := 0;

  http.AllowCookies     := True;
  http.HandleRedirects  := True;
  HTTP.ReadTimeout      := 0;
  HTTP.ConnectTimeout   := 0;

  HTTP.Request.CustomHeaders.clear;
  HTTP.Request.CustomHeaders.Add('user:user');
  HTTP.Request.CustomHeaders.Add('password:password');
  HTTP.request.useragent := 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0';

  try
    http.get(url,Stream);
    hasil := Stream.DataString;
   except

   on E: EIdHTTPProtocolException do
   begin
      hasil := E.ErrorMessage;
      result := hasil;
      exit;
   end;
   on E: Exception do
   begin
      ShowMessage('Failed, ' + E.Message + #13#10 + ' ' + hasil);
   end;
  end;

  result := hasil;
end;


end.

3 times i clicked the button, 3 times new session created

3 times i clicked the button, 3 times new session created

Erwan
  • 202
  • 3
  • 10
  • 10.5.7 is very old, the current version is 10.6.2. In any case, this is clearly a cookie issue. Cookie management has changed alot since 10.1.5 and even 10.5.7, to more closely follow modern cookie practices. So chances are, your old `TIdHTTP` is either discarding the cookies, or is not sending them back to the server, because it thinks they don't actually match the requests being made. That would explain why new sessions are being created on each request. I am not able to reproduce this issue in 10.6.2, sessions get reused as expected. – Remy Lebeau Jun 28 '17 at 19:48

1 Answers1

0

Thankyou for the suggestion Remy, I have upgraded my Indy to 10.6.2 and the problem solved. I followed this tutorial successfully

Erwan
  • 202
  • 3
  • 10