会话没有使用indy 10 idhttp和Delphi XE维护

时间:2017-06-28 11:27:37

标签: delphi session indy delphi-xe indy10

我试图在Delphi XE上使用idhttp 10.5.7创建支持会话的应用程序,但每次调用请求时会话总是会更改。

但是后来我在Delphi 2006中使用相同的代码使用indy 10.1.5会话成功维护,即使我多次调用请求它只创建1个会话。

如何在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次,新会话创建3次

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

1 个答案:

答案 0 :(得分:0)

谢谢你对雷米的建议,我把我的Indy升级到了10.6.2,问题解决了。我成功地关注了this教程