401与Delphi XE中的Indy IdHttp摘要身份验证

401与Delphi XE中的Indy IdHttp摘要身份验证,delphi,indy,digest-authentication,Delphi,Indy,Digest Authentication,尝试使用Delphi XE对合作伙伴的web服务执行get()摘要 我在uses子句中加入了IdAuthenticationDigest,根据我所读的内容,uses子句应该会自动起作用,但我肯定遗漏了什么,因为我得到的是401授权 代码: 您需要设置Request.Username和Request.Password属性,而不是使用Request.Authentication属性。另外,根本不要设置Request.Method或Request.ContentLength属性。所有这三个属性都由TI

尝试使用Delphi XE对合作伙伴的web服务执行get()摘要

我在uses子句中加入了
IdAuthenticationDigest
,根据我所读的内容,uses子句应该会自动起作用,但我肯定遗漏了什么,因为我得到的是401授权

代码:


您需要设置
Request.Username
Request.Password
属性,而不是使用
Request.Authentication
属性。另外,根本不要设置
Request.Method
Request.ContentLength
属性。所有这三个属性都由
TIdHTTP
内部管理

  // Init request:   
  IdHttp := TIdHttp.Create(nil);
  try
    idHttp.Request.ContentType := self.inputType; // 'application/xml'
    idHttp.Request.Accept := self.outputType; //'application/json';

    // Set username and password:
    idHttp.Request.BasicAuthentication := False;
    IdHttp.Request.Username := 'xx';
    IdHttp.Request.Password := 'xx';

    // Send request:
    if Method = 'GET' then
      Result := IdHttp.Get(self.ServiceHost + URI)
    else
    if Method = 'POST' then
      Result := IdHttp.Post(self.ServiceHost + URI, SendStream);
   finally
    IdHttp.Free;
   end;

添加类似以下内容的授权事件:

procedure TForm1.IdHTTP1Authorization(Sender: TObject;
  Authentication: TIdAuthentication; var Handled: Boolean);
begin
Authentication.Username:='user';
Authentication.Password:='passs'; 
if Authentication is TIdDigestAuthentication then
  begin
    showmessage('onAuthorization: '+Authentication.Authentication);
    TIdDigestAuthentication(IdHTTP1.Request.Authentication).Uri:=IdHTTP1.Request.URL;
    TIdDigestAuthentication(Authentication).Method := 'GET';
  end;
Handled:=true;
end;
有时候印第遗漏了一些必要的信息。在我的例子中,我正在连接到Tomcat服务器,但在发送摘要参数身份验证信息时需要Get方法。

在执行Get之前,还需要设置hoInProcessAuth标志

idHttp.HTTPOptions := idHttp.HTTPOptions + [hoInProcessAuth];

谢谢,尝试过了(请求。用户名是我启动它的方式),但仍然失败,401-资源需要身份验证。然后请提供来回实际HTTP流量的跟踪日志。
TIdHTTP
正在发送错误的摘要请求,或者根本没有发送摘要请求。使用数据包嗅探器(如Wireshark)或Indy自己的
TIdLog…
组件捕获该流量。根本不发送摘要请求。在
TIdHTTP.OnSelectAuthorization
事件中,
AuthenticationClass
参数是否在输入时设置为
TIdDigestAuthentication
?如果不是,则要么服务器首先没有请求摘要(您可以在同一事件中使用
AuthInfo
参数进行验证),或者
TIdDigestAuthentication
实际上没有在
TIdHTTP
注册。我刚刚确认
AuthenticationClass
OnSelectAuthorization
事件中被设置为
TIdDigestAuthentication
。我试图命中的API合作伙伴声称组成摘要请求的两个请求中的第二个请求丢失。未触发此事件。请确保IdHttp将basicAuthentication设置为false,并且IdHttp.request.Authentication.Username和IdHttp.request.Authentication.password为空。
idHttp.HTTPOptions := idHttp.HTTPOptions + [hoInProcessAuth];