gpt4 book ai didi

delphi - 如何清除 Indy TIdHTTP BasicAuthentication 凭据?

转载 作者:行者123 更新时间:2023-12-02 05:19:03 25 4
gpt4 key购买 nike

我使用 Indy TIdHTTP 通过 BasicAuthentication 进行获取请求。

代码工作正常,但如果用户使用正确的登录密码重新输入凭据并再次发送请求,TIdHTTP 在第一个 401 之后不会清除 BasicAuthentication 凭据。用户必须登录两次才能授权。

用户操作顺序:

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

我认为第 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;

最佳答案

您应该在更改之前清除您的身份验证

  if Assigned(IdHTTP1.Request.Authentication) then
begin
IdHTTP1.Request.Authentication.Free;
IdHTTP1.Request.Authentication:=nil;
end;

或者你可以这样改变它

  if Assigned(IdHTTP1.Request.Authentication) then
begin
IdHTTP1.Request.Authentication.Username:=...;
IdHTTP1.Request.Authentication.Password:=...;
end else
begin
IdHTTP1.Request.BasicAuthentication:=True;
IdHTTP1.Request.Username:=...;
IdHTTP1.Request.Password:=...;
end;

关于delphi - 如何清除 Indy TIdHTTP BasicAuthentication 凭据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30076775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com