TIdHTTP-在Delphi XE下会话已过期消息 [英] TIdHTTP - session has expired message under Delphi XE

查看:89
本文介绍了TIdHTTP-在Delphi XE下会话已过期消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to port my code from Delphi 2007 to Delphi XE (no Update 1 yet). The problem which I have stumbled on is that under Delphi XE I am getting different response from server after sending second GET message.

HTML格式的消息表明我的会话已过期.但是,到目前为止,相同的代码在Delphi 2007下仍可以正常工作.我已经在互联网上搜索了信息,发现应该使用CookieManager?

The message in formated HTML says that my session has expired. However, the same code works without any problems under Delphi 2007 up to this day. I have searched information over the internet and found out that I should use a CookieManager?

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

The thing is that I am not using any in Delphi 2007 and when I assigned one in Delphi XE the result of my code has not changed. Still I am getting message about expired session.

我还能尝试什么?

更新:我发现一些信息表明Indy 10的cookie存在问题,但已解决.

Update: I have found some info that Indy 10 has problems with cookies but they were fixed.

我已经下载了Indy10_4722快照,很遗憾,该错误仍然出现.

I have downloaded snapshot Indy10_4722, unfortunately the error still occurs.

更新2-提供了代码

因此,我准备了一个示例代码.这与Delphi(2007和XE)都兼容.但是要在2007年下进行编译,您需要具有

So, I've prepared an example code. This is compatibile with both Delphi (2007 and XE). However to compile it under 2007 you need to have GraphicEx library.

该代码正在连接到真实服务器,加载安全图像并以表格形式显示.将图像中的字母重写到编辑框,然后关闭表格.这就是测试所需要做的一切.

The code is connecting to the real server, loads a security image and displays it one the form. Rewrite letters from image to the edit box and close the form. That is all what you need to do to test it.

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.

更新3

Delphi 2007示例的数据包在此处.

Data packets for Delphi 2007 example are here.

Delphi XE示例的数据包在此处.

Data packets for Delphi XE example are here.

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

Free program to analyze packets SmartSniff.

谢谢.

推荐答案

我认为您应该考虑使用诸如 fiddler .

I think you should consider inspecting the request using some tools like httpanalyzer or fiddler.

首先使用Internet Explorer,然后查看它如何执行请求.

First use Internet Explorer and see how it does the request.

然后使用您的应用并比较两个请求.如果是Cookie问题,您会发现问题所在.

Then use your app and compare both requests. If it is a cookie problem you will see what is wrong.

保持活跃不是您的答案.它仅使您不必在与同一服务器的每个请求上都断开连接.参见维基百科

Keep-alive is not you answer. It only makes you connection to not be dropped on each request to the same server. See Wikipedia

这篇关于TIdHTTP-在Delphi XE下会话已过期消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆