I work with Delphi Tokyo and the Indy components that come with it. I need to use a HTTPS REST webservice using NTLM. I always receive a "HTTP/1.1 401 Unauthorized" exception.
1) Any ideas of what I have to do to pass the credentials correctly?
2) Is it a typical reaction to raise an exception? I had expected a simply 401 response string.
uses
SysUtils, Classes,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,
IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL,
IdAuthentication, IdAllAuthentications, IdAuthenticationNTLM,
IdAuthenticationSSPI, IdHeaderList;
function TRestService.GetSSL(AURL: String): String;
var AHTTP : TIdHTTP;
ASSLSocket : TIdSSLIOHandlerSocketOpenSSL;
begin
ASSLSocket := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
ASSLSocket.SSLOptions.Method := sslvSSLv23;
ASSLSocket.SSLOptions.Mode := sslmClient;
AHTTP := TIdHTTP.Create(nil);
AHTTP.IOHandler := ASSLSocket;
AHTTP.ProtocolVersion := pv1_1;
AHTTP.HandleRedirects := True;
AHTTP.Request.ContentType := 'application/json; charset=utf-8';
AHTTP.HTTPOptions := AHTTP.HTTPOptions + [hoKeepOrigProtocol] + [hoInProcessAuth];
AHTTP.Request.Username := Username;
AHTTP.Request.Password := Password;
AHTTP.OnAuthorization := OnAuthorization;
AHTTP.OnSelectAuthorization := OnSelectAuthorization;
try
Result := AHTTP.Get(AURL); // exception
finally
AHTTP.Free;
end;
end;
procedure TRestService.OnAuthorization(Sender: TObject; Authentication: TIdAuthentication; var Handled: Boolean);
begin
// never called
end;
procedure TRestService.OnSelectAuthorization(Sender: TObject; var AuthenticationClass: TIdAuthenticationClass; AuthInfo: TIdHeaderList);
begin
// is called and gives:
// AuthenticationClass.ClassName = TIdSSPINTLMAuthentication
end;