如何清除Indy TIdHTTP BasicAuthentication凭据? [英] How to clear the Indy TIdHTTP BasicAuthentication credentials?

查看:139
本文介绍了如何清除Indy TIdHTTP BasicAuthentication凭据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Indy TIdHTTP通过BasicAuthentication获取请求.

I'm using the Indy TIdHTTP for get request with BasicAuthentication.

代码工作正常,但是如果用户重新输入凭据并使用正确的登录密码再次发送请求,则TIdHTTP不会在第401次后清除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.

用户操作顺序:

步骤1.用户输入错误的登录密码:ResponseCode = 401

Step 1. User type wrong login-password: ResponseCode = 401

步骤2.用户输入正确的登录密码:ResponseCode = 401

Step 2. User type right login-password: ResponseCode = 401

第3步.用户输入正确的登录密码:ResponseCode = 200

Step 3. User type right login-password: ResponseCode = 200

我认为步骤2的结果是一个错误.我该怎么办?

Result on Step 2 is a bug, I think. What should I do?

简单代码:

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 clear your authentication before change

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

或者您可以通过这种方式更改

or you can change it this way

  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;

这篇关于如何清除Indy TIdHTTP BasicAuthentication凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆