2

我的 Delphi 2010 应用程序使用多线程上传内容,上传的数据被 POST 到需要登录的 PHP/Web 应用程序,所以我需要使用共享/全局 cookie 管理器(我正在使用Indy10 Revision 4743),因为 TIdCookieManager 不是线程-安全的 :(

此外,服务器端会话 ID 每 5 分钟自动重新生成一次,因此我必须保持全局和本地 cookie 管理器同步。

我的代码如下所示:

TUploadThread = class(TThread)
// ...

var
   GlobalCookieManager : TIdCookieManager;

procedure TUploadThread.Upload(FileName : String);
var
   IdHTTP           : TIdHTTP;
   TheSSL           : TIdSSLIOHandlerSocketOpenSSL;
   TheCompressor    : TIdCompressorZLib;
   TheCookieManager : TIdCookieManager;
   AStream          : TIdMultipartFormDataStream;
begin
     ACookieManager := TIdCookieManager.Create(IdHTTP);

     // Automatically sync cookies between local & global Cookie managers
     @TheCookieManager.OnNewCookie := pPointer(Cardinal(pPointer( procedure(ASender : TObject; ACookie : TIdCookie; var VAccept : Boolean)
     begin
          OmniLock.Acquire;
          try
             GlobalCookieManager.CookieCollection.AddCookie(ACookie, TIdHTTP(TIdCookieManager(ASender).Owner).URL{IdHTTP.URL});
          finally
                  OmniLock.Release;
          end;    // try/finally

          VAccept := True;
     end )^ ) + $0C)^;
     // ======================================== //


     IdHTTP         := TIdHTTP.Create(nil);
     with IdHTTP do
     begin
          HTTPOptions     := [hoForceEncodeParams, hoNoParseMetaHTTPEquiv];
          AllowCookies    := True;
          HandleRedirects := True;
          ProtocolVersion := pv1_1;

          IOHandler       := TheSSL;
          Compressor      := TheCompressor;
          CookieManager   := TheCookieManager;
     end;    // with

     OmniLock.Acquire;
     try
        // Load login info/cookies
        TheCookieManager.CookieCollection.AddCookies(GlobalCookieManager.CookieCollection);
     finally
            OmniLock.Release;
     end;    // try/finally

     AStream         := TIdMultipartFormDataStream.Create;

     with Stream.AddFile('file_name', FileName, 'application/octet-stream') do
     begin
          HeaderCharset  := 'utf-8';
          HeaderEncoding := '8';
     end;    // with

     IdHTTP.Post('https://www.domain.com/post.php', AStream);
     AStream.Free;
end;

但它不起作用!调用 AddCookies() 时出现此异常

项目 MyEXE.exe 引发异常类 EAccessViolation,并带有消息“地址 00000000 的访问冲突。读取地址 00000000”。

我也尝试使用assign(),即。

 TheCookieManager.CookieCollection.Assign(GlobalCookieManager.CookieCollection);

但我仍然得到同样的例外,通常在这里:

 TIdCookieManager.GenerateClientCookies()

任何人都知道如何解决这个问题?

4

3 回答 3

5

不要对OnNewCookie事件使用匿名过程。改用普通的类方法:

procedure TUploadThread.NewCookie(ASender: TObject; ACookie : TIdCookie; var VAccept : Boolean);
var
  LCookie: TIdCookie;
begin
  LCookie := TIdCookieClass(ACookie.ClassType).Create;
  LCookie.Assign(ACookie);
  OmniLock.Acquire; 
  try 
    GlobalCookieManager.CookieCollection.AddCookie(LCookie, TIdHTTP(TIdCookieManager(ASender).Owner).URL); 
  finally 
    OmniLock.Release; 
  end;
  VAccept := True;
end;

或者:

procedure TUploadThread.NewCookie(ASender: TObject; ACookie : TIdCookie; var VAccept : Boolean);
begin
  OmniLock.Acquire; 
  try 
    GlobalCookieManager.CookieCollection.AddServerCookie(ACookie.ServerCookie, TIdHTTP(TIdCookieManager(ASender).Owner).URL); 
  finally 
    OmniLock.Release; 
  end;
  VAccept := True;
end;

然后像这样使用它:

procedure TUploadThread.Upload(FileName : String); 
var 
  IdHTTP           : TIdHTTP; 
  TheSSL           : TIdSSLIOHandlerSocketOpenSSL; 
  TheCompressor    : TIdCompressorZLib; 
  TheCookieManager : TIdCookieManager; 
  TheStream        : TIdMultipartFormDataStream; 
begin 
  IdHTTP := TIdHTTP.Create(nil); 
  try
    ...
    TheCookieManager := TIdCookieManager.Create(IdHTTP); 
    TheCookieManager.OnNewCookie := NewCookie;

    with IdHTTP do 
    begin 
      HTTPOptions     := [hoForceEncodeParams, hoNoParseMetaHTTPEquiv]; 
      AllowCookies    := True; 
      HandleRedirects := True; 
      ProtocolVersion := pv1_1; 

      IOHandler       := TheSSL; 
      Compressor      := TheCompressor; 
      CookieManager   := TheCookieManager; 
    end;    // with 

    OmniLock.Acquire; 
    try 
      // Load login info/cookies 
      TheCookieManager.CookieCollection.AddCookies(GlobalCookieManager.CookieCollection); 
    finally 
      OmniLock.Release; 
    end;

    TheStream := TIdMultipartFormDataStream.Create; 
    try
      with TheStream.AddFile('file_name', FileName, 'application/octet-stream') do 
      begin 
        HeaderCharset  := 'utf-8'; 
        HeaderEncoding := '8'; 
      end;

      IdHTTP.Post('https://www.domain.com/post.php', TheStream); 
    finally
      TheStream.Free; 
    end;
  finally
    IdHTTP.Free;
  end;
end; 
于 2012-05-03T21:51:40.660 回答
3

如果我不得不猜测,我会说你的问题出在某个地方:

 // Automatically sync cookies between local & global Cookie managers
 @TheCookieManager.OnNewCookie := pPointer(Cardinal(pPointer( procedure(ASender : TObject; ACookie : TIdCookie; var VAccept : Boolean)
 begin
      OmniLock.Acquire;
      try
         GlobalCookieManager.CookieCollection.AddCookie(ACookie, TIdHTTP(TIdCookieManager(ASender).Owner).URL{IdHTTP.URL});
      finally
              OmniLock.Release;
      end;    // try/finally

      VAccept := True;
 end )^ ) + $0C)^;

我不确定这个$0C神奇的数字有什么用,但我敢打赌所有这些演员都在那里,因为你花了很长时间让编译器接受这个。它给您输入错误,说您无法将一件事分配给另一件事。

这些类型错误是有原因的!如果你绕过类型系统,事情很可能会崩溃。尝试将该匿名方法转换为 TUploadThread 上的普通方法并以这种方式分配它,看看它是否工作得更好。

于 2012-05-03T21:40:56.823 回答
2

回复评论:

谢谢大家,我转换为普通方法,但我仍然在 AddCookies() 中遇到异常,最后一个发生在读取 FRWLock.BeginWrite; 的行中。在这个过程中 TIdCookies.LockCookieList(AAccessType: TIdCookieAccess): TIdCookieList;

如果您的错误是 Access Violation with Read of address 00000000,则具有非常具体的含义。这意味着您正在尝试对nil的对象做某事。

当你得到它时,中断调试器。如果错误发生在您所说的发生的行上,那么几乎可以肯定,此时要么Self要么FRWLock为零。检查这两个变量并找出尚未构建的变量,这将为您指明解决方案。

于 2012-05-03T22:15:19.117 回答