Globaler, threadsicherer Cookie-Manager mit Indy
Meine Delphi 2010-App lädt Inhalte mithilfe von Multithreading hoch. Hochgeladene Daten werden an eine PHP- / Webanwendung weitergeleitet, für die eine Anmeldung erforderlich ist. Daher muss ein freigegebener / globaler Cookie-Manager verwendet werden (ich verwendeIndy10 Revision 4743) da TIdCookieManager nicht threadsicher ist :(
Auch serverseitig wird die Sitzungs-ID automatisch alle 5 Minuten neu generiert, so dass ichMuss Halten Sie die globalen und lokalen Cookie-Manager synchron.
Mein Code sieht so aus:
<code>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; </code>
Aber es geht nicht! Ich erhalte diese Ausnahme beim Aufruf von AddCookies ()
Project MyEXE.exe hat die Ausnahmeklasse EAccessViolation mit der Meldung "Zugriffsverletzung bei Adresse 00000000. Read of address 00000000" ausgelöst.
Ich habe auch versucht mit assign (), dh.
<code> TheCookieManager.CookieCollection.Assign(GlobalCookieManager.CookieCollection); </code>
Aber ich bekomme immer noch die gleiche Ausnahme, normalerweise hier:
<code> TIdCookieManager.GenerateClientCookies() </code>
Weiß jemand, wie man das behebt?