Link to home
Start Free TrialLog in
Avatar of Thomas
Thomas

asked on

THTTPRIO username and password

The code below is executed when a user clicks a button.
It asks the user for a username/password and tries to upload a file to a webservice.
When the username/password combination is correct, everything works fine. But when the user types an incorrect username/password combination, this functions crashes. ( which is normal, because authentication failed...) .
After such a crash, the code no longer works, even after rerunning the same procedure this time with the correct username/password combination.

The only solution I found is to restart the whole application... There has to be a better way?
Does the HTTPRIO object cache my incorrect username/password? Or .... ?



RIO := THTTPRIO.Create(nil);
RIO.HTTPWebNode.UserName :=Username;
RIO.HTTPWebNode.Password :=PassWord;
webserver := GetCRS( True, '' , RIO );

 // get file to upload and encode it in base64
 myFileToUpload := TByteDynArray( TEncoding.UTF8.GetBytes( xml.XML.Text ) );

// create upload file request object
myUploadFileRequest := UploadFileRequest.Create;
myUploadFileRequest.UploadFile := PayLoadType( myFileToUpload );

// upload file to server
 myUploadFileResponse := webserver.uploadFile_A2A( myUploadFileRequest ); --> Crash

Open in new window

Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

First of all I'd put the code between a Try..finally statement and also you should use a try..except statement just to handle exceptions and free correctly the previous created Rio object
Something like
RIO := THTTPRIO.Create(nil);
  try
    RIO.HTTPWebNode.UserName := UserName;
    RIO.HTTPWebNode.Password := Password;
    try
      webserver := GetCRS(True, '', RIO);
      // get file to upload and encode it in base64
      myFileToUpload := TByteDynArray(TEncoding.UTF8.GetBytes(xml.xml.Text));
      // create upload file request object
      myUploadFileRequest := UploadFileRequest.Create;
      myUploadFileRequest.UploadFile := PayLoadType(myFileToUpload);
      // upload file to server
      myUploadFileResponse := webserver.uploadFile_A2A(myUploadFileRequest);
    except
      // Add here whatever message or code you want
    end;
  finally
    RIO.Free; // Free object
  end;

Open in new window

Avatar of Thomas
Thomas

ASKER

Changed the code to the suggestion of Ferruccio Accalai, but still no result.

Added onBeforePost and onBeforeExecute ... Both procedure don't even get called ...
Is there anyone who can give me a clue how to solve this issue?
ASKER CERTIFIED SOLUTION
Avatar of Thomas
Thomas

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
Avatar of Thomas

ASKER

Found the answer myself.