I'm using the Indy TIdHTTP for get request with BasicAuthentication.
Code working fine, but TIdHTTP doesn't clearing the BasicAuthentication credentials after first 401, if user retype the credentials and send request again, with right login-password. User must login twice to authorize.
User action sequence:
Step 1. User type wrong login-password: ResponseCode = 401
Step 2. User type right login-password: ResponseCode = 401
Step 3. User type right login-password: ResponseCode = 200
Result on Step 2 is a bug, I think. What should I do?
Simple code:
var
IdHTTP1: TIdHTTP;
fLogin : string;
fPassword : string;
/// ...
if ( fLogin <> '' ) and ( fPassword <> '' )
then
begin
if ( IdHTTP1.Request.Username <> fLogin )
or
( IdHTTP1.Request.Password <> fPassword )
then
begin
IdHTTP1.Request.BasicAuthentication := True;
IdHTTP1.Request.Username := fLogin;
IdHTTP1.Request.Password := fPassword;
end;
s := IdHTTP1.Get( 'some_url' );
response_code := Idhttp1.response.ResponseCode;
case response_code of
200:
begin
// parse request data
end;
401 : Result := nc_res_Auth_Fail;
else Result := nc_res_Fail;
end;
end;
You should set the
Request.UserName
andRequest.Password
properties on each request, and then use theOnAuthorization
event to retrieve new credentials if the server asks for them, eg:TIdHTTP
will internally keep re-trying login, triggeringOnAuthorization
each time, until the server stops sending a 401 reply orTIdHTTP.MaxAuthRetries
has been reached, whichever occurs first.You should clear your authentication before change
or you can change it this way