Delphi Indy10 Cookie Session Problem

Hey,

I am using the latest version of indy10 with delphi 7.

I have 2 functions GET and POST. I am using a global TIdCookieManager variable so the session will be carried on into GET and POST functions. It seems the cookies are saved into the cookiemanager when I do my first GET request for the webpage but when I do my POST it never sends those cookies stored in the global cookiemanager variable. What am I missing?

function TBaseThread.doPost(url: string; params: string; useragent: string; UseProxy:Integer) : string;
var
http:TIDHttp;
html:String;
cookiestr:string;
code:integer;
myssl: TIdSSLIOHandlerSocketOpenSSL;
i:integer;
list:TSplitArray;
boolretry:boolean;
retries:integer;
sl:TStringList;
Data: TStringList;
mysocks:TIdSocksInfo;
label
retry;
begin
repeat
application.processmessages;
sleep(100);
until paused[myindex]=false;
Result := '';
//creates;
http := tIdHttp.Create(nil);
Data := TStringList.Create;


//setup
retries := 1;

//HTTP.Request.ContentType:='application/x-www-form-urlencoded';
//HTTP.Request.Connection:='Keep-Alive';
//http.HTTPOptions := http.HTTPOptions -[hoForceEncodeParams];
http.Request.Method := Id_HTTPMethodPost;
HTTP.Request.UserAgent := useragent;
HTTP.HandleRedirects := true;
HTTP.AllowCookies := True;
HTTP.CookieManager := TheCookie;
http.ProtocolVersion := pv1_1;
//creates:
mysocks := TIdSocksInfo.Create;
myssl := TIdSSLIOHandlerSocketOpenSSL.Create(http);

myssl.SSLOptions.Method := sslvTLSv1;
http.IOHandler := myssl;

boolretry := false;



//Set Time Outs
http.ReadTimeout := ReadTimeout;
http.ConnectTimeout := ConnectTimeout;

//setup proxy
    if UseProxy = 1 then
      begin
        if(ptype = 'socks4')then
          begin
            if (pUser <> '') and (pPass <> '') then
              begin
           // http.Socket.SocksInfo.Authentication := saUsernamePassword;
            mysocks.Host := pHost;
            mysocks.Port := Strtoint64(pPort);
            mysocks.Username := pUser;
            mysocks.Password := pPass;
            mysocks.Version := svSocks5;
            mysocks.authentication := saUsernamePassword;
            http.Socket.TransparentProxy := mysocks;
            end;//if username and pass
            mysocks.Host := pHost;
            mysocks.Port := Strtoint64(pPort);
            mysocks.Version := svSocks4;
            mysocks.authentication := saNoAuthentication;
            http.Socket.TransparentProxy := mysocks;
          end else begin
            if (pUser <> '') and (pPass <> '') then
              begin
                http.ProxyParams.BasicAuthentication := true;
                http.ProxyParams.ProxyServer := pHost;
                http.ProxyParams.ProxyPort := Strtoint64(pPort);
                http.ProxyParams.ProxyUsername := pUser;
                http.ProxyParams.ProxyPassword := pPass;
              end else begin
                http.ProxyParams.ProxyServer := pHost;
                http.ProxyParams.ProxyPort := Strtoint64(pPort);
                http.ProxyParams.ProxyUsername := '';
                http.ProxyParams.ProxyPassword := '';
              end;
          end;
      end;


{
      for i := 0 to thecookie.CookieCollection.Count-1 do begin
    //   addlog(thecookie.CookieCollection.Cookies[i].CookieText);
      cookiestr := cookiestr+thecookie.CookieCollection.Cookies[i].CookieText;
      end;    }

//if(loggedin) then begin
{
try
for i := 0 to Cookie.Count-1 do begin
cookiestr := cookiestr+ copy(Cookie.Strings[i],1,pos(';',Cookie.Strings[i]))+ ' ';

end;
if(length(cookiestr) > 2) then HTTP.Request.CustomHeaders.add('Cookie: '+cookiestr);

except
addlog('Error Loading Cookie in POST');
end;  }
//end;//if

retry:
try

list:=arrsplit(params,'&');
for i := 0 to high(list) do begin
data.add(list[i]);
end;

Result := http.Post(url,data);

Data.Free;
except
 on E: Exception do begin
code := http.ResponseCode;
Addlog(Inttostr(code) + ' ' + e.Message);
boolretry := false;
if(retries < retryattempts) then begin
addlog('Retrying...POST');
 inc(retries);
 sleep(1000);
 boolretry := true;
end;
end;
end;

//retry boolean
if(boolretry) then begin
boolretry := false;
goto retry;
end;

//save cookie
{try
cookie.clear;
for i := 0 to thecookie.CookieCollection.Count-1 do begin
cookie.add(thecookie.CookieCollection.Cookies[i].CookieText);
end;
except
addlog('Saving cookie error');
end;  }


//write debug
  if(debugmode) then
    begin
      if(not(directoryexists('debug')))then
        begin
          createdir('debug');
        end;
      sl := tstringlist.create;
      sl.Text := result;
      sl.Add(url);
      sl.Add(params);
      sl.SaveToFile('debug/POST_'+email+'_Action_'+inttostr(debugpos)+'_.html');
      sl.free;
      inc(debugpos);
    end;

//TheCookie.Free;
myssl.Free;
mysocks.free;
http.free;
end;

Open in new window

nickdelphi777Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jimyXCommented:
Most of your code is commented out, so I am not sure I can follow what you are after.

Normally, when you do GET the server sends the page requested with some cookies, and when you follow with POST, the CookieManager still keeps those cookies, so you do not have to insert them yourself.

Unless you are doing something different (i.e sending some cookies that GET did not have), then you can use:
TIdHTTP.CookieManager.AddServerCookie or TIdHTTP.CookieManager.AddServerCookies.

Try to slice simple example that explains what you need so it can be easily tested.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.