我正在尝试将我的代码从 Delphi 2007 移植到 Delphi XE(尚未更新 1)。我偶然发现的问题是,在 Delphi XE 下,我在发送第二条 GET 消息后从服务器得到不同的响应。
格式化 HTML 中的消息表明我的会话已过期。然而,相同的代码迄今为止在 Delphi 2007 下运行没有任何问题。我在互联网上搜索了信息,发现我应该使用 CookieManager?
问题是我没有在 Delphi 2007 中使用任何一个,当我在 Delphi XE 中分配一个时,我的代码结果没有改变。我仍然收到有关会话过期的消息。
我还能尝试什么?
Update:我发现一些信息表明 Indy 10 有 cookie 问题,但已修复。
我已经下载了Indy10_4722快照,不幸的是错误仍然发生。
更新 2 - 提供代码
所以,我准备了一个示例代码。这与 Delphi(2007 和 XE)兼容。然而,要在 2007 年下编译它,你需要有.
该代码连接到真实服务器,加载安全图像并将其显示在表单中。将图像中的字母重写到编辑框并关闭表单。这就是测试它所需要做的全部事情。
program IndyTest;
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Contnrs, Menus, ExtCtrls, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,
{$IFDEF VER220}PngImage{$ELSE}GraphicEx{$ENDIF}, StrUtils;
{$R *.res}
procedure LoadSecurityImage(AImage: TImage; AIdHTTP: TIdHTTP; AImgLink: String);
var
PNGGraphic: {$IFDEF VER220}TPngImage{$ELSE} TPNGGraphic{$ENDIF};
ResponseStream: TMemoryStream;
begin
ResponseStream := TMemoryStream.Create;
PNGGraphic := {$IFDEF VER220}TPngImage.Create{$ELSE}TPNGGraphic.Create{$ENDIF};
try
AIdHTTP.Get(AImgLink, ResponseStream);
ResponseStream.Position := 0;
PNGGraphic.LoadFromStream(ResponseStream);
AImage.Picture.Assign(PNGGraphic);
finally
ResponseStream.Free;
PNGGraphic.Free;
end;
end;
function GetImageLink(AIdHTTP: TIdHTTP): String;
var
WebContentStream: TStringStream;
Index, Index2: Integer;
begin
Result := '';
WebContentStream := TStringStream.Create('');
try
AIdHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
AIdHTTP.Get('http://czat.wp.pl/i,1,chat.html', WebContentStream);
Index := Pos('id="secImg">', WebContentStream.DataString);
if Index > 0 then
begin
Index := PosEx('src="', WebContentStream.DataString, Index) + 5;
Index2 := PosEx('">', WebContentStream.DataString, Index);
if Index > 10 then
begin
Result := Copy(WebContentStream.DataString, Index, Index2 - Index);
end;
end;
finally
WebContentStream.Free;
end;
end;
procedure CheckForContent(const ANick, AImageSeed: String; AIdHTTP: TIdHTTP);
var
WebContent: TStringStream;
S: String;
begin
WebContent := TStringStream.Create('');
try
AIdHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
S := 'http://czat.wp.pl/chat.html?i=31179&auth=nie&nick=' + ANick
+ '®ulamin=tak&simg=' + AImageSeed + '&x=39&y=13';
AIdHTTP.Get(S, WebContent);
if Pos('<div class="applet">', WebContent.DataString) > 0 then
ShowMessage('It works properly.')
else if Pos('<div id="alert">Sesja wygas', WebContent.DataString) > 0 then
ShowMessage('Session expired')
else
ShowMessage('Unknown result.');
finally
WebContent.Free;
end;
end;
var
LogForm: TForm;
SecurityImage: TImage;
Edit: TEdit;
IdHTTPWp: TIdHTTP;
begin
Application.Initialize;
IdHTTPWp := TIdHTTP.Create(Application);
IdHTTPWp.AllowCookies := True;
IdHTTPWp.HandleRedirects := True;
IdHTTPWp.HTTPOptions := [hoForceEncodeParams];
LogForm := TForm.Create(Application);
LogForm.Position := poScreenCenter;
SecurityImage := TImage.Create(LogForm);
SecurityImage.Parent := LogForm;
SecurityImage.AutoSize := True;
Edit := TEdit.Create(LogForm);
Edit.Parent := LogForm;
Edit.Top := 64;
LoadSecurityImage(SecurityImage, IdHTTPWp, GetImageLink(IdHTTPWp));
LogForm.ShowModal;
CheckForContent('TestUser', Edit.Text, IdHTTPWp);
Application.Run;
end.
Update 3
Delphi 2007 的数据包示例are here http://speedy.sh/g6MYM/Delphi2007.ssp.
Delphi XE 数据包示例are here http://speedy.sh/jmHaH/DelphiXE.ssp.
分析数据包的免费程序智能嗅探 http://www.nirsoft.net/utils/smsniff.html.
Thanks.