• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 505
  • Last Modified:

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

0
nickdelphi777
Asked:
nickdelphi777
1 Solution
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now