IdHTTP基本身份验证访问冲突

时间:2012-09-15 20:47:35

标签: delphi delphi-2010 indy idhttp

我正在尝试使用Indy HTTP类进行HTTP身份验证。但由于某些未知原因,我在此行中收到了访问冲突错误: IdHTTP1.Request.Authentication.Username:=用户名;

代码延伸是:

  IdHTTP1:= TIdHttp.Create(Application);
  IdHTTP1.ConnectTimeout:= 10000;
  IdHTTP1.Request.Clear;
  IdHTTP1.Request.BasicAuthentication:= true;
  IdHTTP1.Request.Authentication.Username := Username;
  IdHTTP1.Request.Authentication.Password := Password;
      try
        IdHTTP1.Get(PbxURL);
        HttpCode := IdHTTP1.ResponseCode;
      except
        on E: EIdHTTPProtocolException do
          HttpCode := IdHTTP1.ResponseCode;

我正在使用Delphi 2010,并且已经尝试过这样的事情: IdHTTP1.Request.Authentication.Username:='admin'; 但没有解决问题...

2 个答案:

答案 0 :(得分:15)

通过快速检查,当IdHTTP.Request.Authentication为真时,似乎不需要Request.BasicAuthentication(因此没有创建)。您应该使用Request.UserNameRequest.Password代替。

IdHTTP1:= TIdHttp.Create(Application);
IdHTTP1.ConnectTimeout:= 10000;
IdHTTP1.Request.Clear;
IdHTTP1.Request.BasicAuthentication:= true;
IdHTTP1.Request.UserName := UserName;
IdHTTP1.Request.Password := Password;

答案 1 :(得分:2)

默认情况下,在发送请求并收到身份验证响应之前,不会分配Request.Authentication对象,然后触发OnSelectAuthorization事件以确定要为对象分配的类类型对于后续请求..

可以分配Request.Authentication对象的唯一方法是,如果您在发送请求之前在自己的代码中手动执行此操作,例如,如果您事先知道服务器使用哪个身份验证方案而不发送请求动态发现。