views:

1205

answers:

3
+2  A: 

I don't see where you instantiate CookieManager, but that's where you should keep track of the session. The server will send some cookie that represents the current session, and all further requests that you send to the server should include that cookie so the server knows which session to use.

You'll have to either keep the cookie-manager object around for the duration of the session, or you'll have to save its data somewhere else and then re-load it each time you create a new cookie-manager object. I'd prefer the former. In fact, you might consider keeping the entire HTTP object around.

Rob Kennedy
I forgot to copy over the code for that.. I had tried putting the cookie manager in the onCreate event, and then leaving it around (not nil'ing it) but it didn't seem to work. As for keeping the HTTP object around, that was something I was not really wanting to do, but I could do it I guess. I'll see what it does when I keep the http object around. I didn't really want to do that (no perticular reasoning)....
Brad
+1  A: 

As Rob said your TIdCookieManager is key for maintaining a Cookie based session. The TIdCookieManager could be created in a datamodule's create event or the mainforms OnCreate() event and then set every time you create a IdHTTP component.

K.Sandell
I will try that OnCreate again, I had it as OnCreate on the form. (BTW, thanks for the code before, it worked wonderfully up until I had to deal with this site and maintaining a session.)
Brad
No problem, glad to be of help! - Have you looked into using threads for the communication parts? Might make for a smoother ride if it can be implemented in your code?
K.Sandell
Yeah, I've been looking at a threaded demo, but threading is just over my current knowledge level in Delphi. And a lot of what this project does is contact the same website over and over again, so I don't need/want multiple request for this one. But I use the same base code in a couple other projects, and it would be nice to thread them. Do you have any suggestions on a good thread demo?
Brad
This is a rather large topic, so a new question might be the best way to go. I can definitely help out on this, but the exact implementation can be different depending on usage and needs...
K.Sandell
+1  A: 

As you mentioned in your comments, you are creating CookieManager in OnCreate event-handler, so that when TForm1.webSession is called, CookieManager is available, but in the finally block of TForm1.webSession you are freeing CookieManager, so once you leave TForm1.webSession method, CookieManager is out of memory. So, next time TForm1.webSession is called, CookieManager is Nil, and no cookie is saved for you.

There are two other notes that are not related to your question, but are related to your source code:

1- Your method is returning AnsiString, but you are using Utf8ToWideString for assigning value to Result variable. Utf8ToWideString returns WideString, so compiler has to convert WideString to AnsiString, and not only this reduces the performance, but also it loses the unicode characters in the returning string. You should change your method signature to return either String (D2009 & D2010) or WideString (Older versions of Delphi).

2- You don't need to check if SStream, AntiFreeze, or HTTPCon are assigned in the finally block. You can simply call the Free method, or use FreeAndNil procedure.

Regards

vcldeveloper
Thank you for the info on the AnsiString, I'll edit the working code to fix this. In the code that I had the CookieManager in the OnCretae event I didn't have the freeandnil code in the the that version.
Brad