简体   繁体   English

401使用Delphi XE中的Indy IdHttp摘要式身份验证

[英]401 With Indy IdHttp Digest Authentication in Delphi XE

Trying to do a get() with Digest to a partner's web service with Delphi XE. 尝试用Delphi XE对合作伙伴的Web服务进行Digest的get()。

I have included IdAuthenticationDigest to the uses clause which should automatically work from what I've read - but I must be missing something because I'm getting a 401 Unauthorized. 我已经将IdAuthenticationDigest包含在uses子句中,该子句应该从我读过的内容中自动运行 - 但我必须丢失一些东西,因为我得到了401 Unauthorized。

Code: 码:

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

    // Set request method:
    idHttp.Request.Method := Method; // 'Get'
    // Set username and password:
    idHttp.Request.BasicAuthentication := False;
    // IdHttp.Request.Username/Password also fails
    IdHttp.Request.Authentication.Username := 'xx';
    IdHttp.Request.Authentication.password := 'xx';

    IdHttp.Request.ContentLength := Length(Body);

    // 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;
end;

You need to set the Request.Username and Request.Password properties instead of using the Request.Authentication property. 您需要设置Request.UsernameRequest.Password属性,而不是使用Request.Authentication属性。 Also, do not set the Request.Method or Request.ContentLength properties at all. 另外,根本不要设置Request.MethodRequest.ContentLength属性。 All three of those properties are managed by TIdHTTP internally. 所有这三个属性都由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;

Add OnAuthorization event something like this: 添加OnAuthorization事件,如下所示:

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;

Sometimes indy misses some necesary info. 有时indy错过了一些必要的信息。 In my case, I'm connecting to a Tomcat server but this need the Get method when the digest params authentication info is sent. 在我的情况下,我正在连接到Tomcat服务器,但是当发送摘要参数身份验证信息时,这需要Get方法。

您还需要设置hoInProcessAuth标志,以便执行GET。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM