2

尝试使用 Digest 对合作伙伴的使用 Delphi XE 的 Web 服务执行 get()。

我已将IdAuthenticationDigestuses 子句包含在我所阅读的内容中,该子句应该会自动起作用-但我一定遗漏了一些东西,因为我得到了401 Unauthorized。

代码:

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;
4

3 回答 3

4

您需要设置Request.UsernameandRequest.Password属性而不是使用该Request.Authentication属性。此外,根本不要设置Request.MethodorRequest.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;
于 2012-08-21T16:23:29.170 回答
4

添加类似这样的 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;

有时 indy 会遗漏一些必要的信息。就我而言,我正在连接到 Tomcat 服务器,但这需要在发送摘要参数身份验证信息时使用 Get 方法。

于 2012-10-16T21:53:07.023 回答
2

在执行 GET 之前,您还需要设置hoInProcessAuth标志。

idHttp.HTTPOptions := idHttp.HTTPOptions + [hoInProcessAuth];
于 2014-08-06T15:07:35.793 回答