我使用Indy TIdHTTP獲取BasicAuthentication請求。如何清除Indy TIdHTTP BasicAuthentication憑據?
代碼正常工作,但如果用戶使用正確的登錄密碼重新輸入憑證並再次發送請求,則TIdHTTP在首次401之後不會清除BasicAuthentication憑據。用戶必須登錄兩次才能授權。
用戶操作序列:
步驟1.用戶類型錯誤的登錄密碼:ResponseCode = 401
步驟2.用戶類型右登錄密碼:ResponseCode = 401
步驟3 。用戶類型權限登錄密碼:ResponseCode = 200
我認爲,步驟2的結果是一個錯誤。我該怎麼辦?
簡單代碼:
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;
我會試試看,謝謝! –