Advertisement
gdhami

Indy login and file upload

Mar 22nd, 2012
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.27 KB | None | 0 0
  1. var
  2.    GlobalCookieManager : TIdCookieManager;
  3.  
  4. // ...
  5. //------------------------------------------------------------------------//
  6. procedure LoginToServer(TheLogin, ThePassword : String);
  7. var
  8.    IdHTTP      : TIdHTTP;
  9.    ParamList   : TStringList;
  10. begin
  11.      IdHTTP                      := TIdHTTP.Create(nil);
  12.      try
  13.         // First reset CookieManager
  14.         FreeAndNil(GlobalCookieManager);
  15.  
  16.         GlobalCookieManager  := TIdCookieManager.Create(nil);
  17.         IdHTTP.CookieManager := GlobalCookieManager;
  18.  
  19.         ParamList                := TStringList.Create;
  20.         try
  21.            ParamList.Values['login']    := TheLogin;
  22.            ParamList.Values['password'] := ThePassword;
  23.  
  24.            IdHTTP.Post('http://localhost/my-app/login.php', ParamList);
  25.         finally
  26.                FreeAndNil(ParamList);
  27.         end;    // try/finally
  28.      finally
  29.             FreeAndNil(IdHTTP);
  30.      end;    // try/finally
  31. end;
  32. //------------------------------------------------------------------------//
  33. procedure TUploadThread.UploadFile(FileName : String);
  34. var
  35.    IdHTTP      : TIdHTTP;
  36.    CookieMgr   : TIdCookieManager;
  37.    RangeStream : TIdHTTPRangeStream;
  38.    StartPos    : Int64;
  39. begin
  40.      IdHTTP                  := TIdHTTP.Create(nil);
  41.      try
  42.         CookieMgr            := TIdCookieManager.Create(nil);
  43.         IdHTTP.CookieManager := CookieMgr;
  44.         with IdHTTP.Request.RawHeaders do
  45.         begin
  46.              Clear;
  47.              for i := 0 to GlobalCookieManager.CookieCollection.Count - 1 do
  48.                  Add('Cookie' + NameValueSeparator + GlobalCookieManager.CookieCollection.Cookies[i].CookieText);
  49.         end;    // with
  50.  
  51.         TheFile              := TFileStream.Create(FileName, fmOpenRead OR fmShareDenyWrite);
  52.         RangeStream          := TIdHTTPRangeStream.Create(TheFile, StartPos, -1, True);
  53.         if (RangeStream.ResponseCode = 206) then
  54.         begin
  55.              try
  56.                 IdHTTP.Post('http://localhost/my-app/upload.php', RangeStream);
  57.              finally
  58.                     FreeAndNil(RangeStream);
  59.              end;    // try/finally
  60.         end;
  61.      finally
  62.             FreeAndNil(IdHTTP);
  63.      end;    // try/finally
  64. end;
  65. //------------------------------------------------------------------------//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement