Link to home
Start Free TrialLog in
Avatar of nickdelphi777
nickdelphi777

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of jimyX
jimyX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial