TIdHTTP - 会话已在Delphi XE下过期消息

Wod*_*dzu 11 delphi port indy delphi-2007 delphi-xe

我正在尝试将我的代码从Delphi 2007移植到Delphi XE(尚未更新1).我偶然发现的问题是,在Delphi XE下,我在发送第二条GET消息后得到了与服务器不同的响应.

格式化HTML中的消息表明我的会话已过期.但是,直到今天,相同的代码在Delphi 2007下仍然没有任何问题.我通过互联网搜索信息,发现我应该使用CookieManager?

问题是我在Delphi 2007中没有使用任何东西,当我在Delphi XE中分配一个时,我的代码的结果没有改变.我仍然收到有关过期会话的消息.

我还能尝试什么?

更新:我发现一些信息,Indy 10有cookie问题,但它们已修复.

我已经下载了快照Indy10_4722,遗憾的是错误仍然存​​在.

更新2 - 提供的代码

所以,我准备了一个示例代码.这与Delphi(2007和XE)兼容.但是要在2007年编译它,你需要有GraphicEx库.

代码连接到真实服务器,加载安全映像并在表单中显示它.将图像中的字母重写到编辑框并关闭表单.这就是测试它所需要做的所有事情.

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
      + '&regulamin=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.
Run Code Online (Sandbox Code Playgroud)

更新3

Delphi 2007示例的数据包就在这里.

Delphi XE示例的数据包就在这里.

免费程序来分析SmartSniff数据包.

谢谢.

Raf*_*cci 1

我认为您应该考虑使用httpanalyzerfiddler等工具检查请求。

首先使用 Internet Explorer 并查看它如何执行请求。

然后使用您的应用程序并比较这两个请求。如果是 cookie 问题,您就会看到问题所在。

保持活动不是你的答案。它只会使您的连接不会在对同一服务器的每个请求上被丢弃。参见维基百科