使用 indy 10 idhttp 和 Delphi XE 不维护会话
Session not maintained using indy 10 idhttp and Delphi XE
我正在尝试在 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 次新会话
谢谢Remy的建议,我已经将Indy升级到10.6.2,问题解决了。我已成功完成 this 教程
我正在尝试在 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 次新会话
谢谢Remy的建议,我已经将Indy升级到10.6.2,问题解决了。我已成功完成 this 教程