我正在使用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;

最佳答案

更改之前,您应先清除身份验证

  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/

10-10 14:25