How to Do Basic Type Authentication with Delphi XE8

Asked

Viewed 677 times

1

I am developing an application in Delphi xe8 that will consume web service data that returns data in json. The problem is that it is presenting the message

HTTP/1.1 401 Unauthorized
{"code":401,"message":"Unauthorized","detailedMessage":"Unauthorized"}.

How do I submit this basic type authorization.

Current function:

procedure TForm1.executaConexao;
var
  IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
  RequestBody: TStream;
  ResponseBody, headAutorization, credencial: string;
begin
  try
    // IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
    IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Mode := sslmClient;
    IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method := sslvSSLv2;
    try
      // IdHTTP1.Request.Accept := 'application/json';
      IdHTTP1.Request.ContentType := 'application/json';
      IdHTTP1.Request.CharSet := 'charset=utf-8';
      IdHTTP1.Request.Connection := 'keep-alive';
      IdHTTP1.Request.UserAgent :=
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36';
      IdHTTP1.Request.AcceptEncoding := 'gzip, deflate';
      IdHTTP1.Request.AcceptLanguage := 'pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4';
      IdHTTP1.Request.CacheControl := 'no-cache';
      IdHTTP1.Request.BasicAuthentication := true;
      IdHTTP1.Request.Username := 'usuarioXXXX';
      IdHTTP1.Request.Password := 'testesenha';
      credencial := getCredencial(IdHTTP1.Request.Username,
        IdHTTP1.Request.Password);
      // IdHTTP1.Request.CustomHeaders.Delimiter := ',';
      IdHTTP1.Request.CustomHeaders.FoldLines := true;
      IdHTTP1.Request.CustomHeaders.Values['Authorization '] := 'Basic ' +
        credencial;
      IdHTTP1.Request.CustomHeaders.Values['codColigada'] := IntToStr(4);
      IdHTTP1.Request.CustomHeaders.Values['codSistema'] := 'S';
      IdHTTP1.Request.CustomHeaders.Values['codUsuario'] := 'integraTotvs';
      IdHTTP1.Response.ContentType := 'application/json';
      IdHTTP1.Response.CharSet := 'UTF-8';
      IdHTTP1.AllowCookies := true;
      IdHTTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
      IdHTTP1.HandleRedirects := true;
      IdHTTP1.HTTPOptions := [hoInProcessAuth] + [hoKeepOrigProtocol] +
        [hoForceEncodeParams];
      IdHTTP1.ProtocolVersion := pv1_0;
      ResponseBody :=
        IdHTTP1.Get
        ('10.109.1.43:8051/rmsrestdataserver/rest/EduPSRhuPessoaData/…');
      // Memo1.Lines.Add(ResponseBody);
      Memo1.Lines.Add(IdHTTP1.ResponseText);
    except
      on E: EIdHTTPProtocolException do
      begin
        Memo1.Lines.Add(E.Message);
        Memo1.Lines.Add(E.ErrorMessage);
      end;
      on E: exception do
      begin
        Memo1.Lines.Add(E.Message);
      end;
    end;
  finally
    // HTTP.Free;
  end;
end;

1 answer

0

Answering the main question, to send a BASIC authentication using Indy, can be used a function similar to the one below (example of GET):

function Comunicar(url, token: String): Boolean;
var
  IdHTTP1: TIdHttp;
begin
  IdHTTP1:= TIdHttp.Create(nil);
  IdHTTP1.ConnectTimeout:= 10000;
  IdHTTP1.Request.Clear;
  IdHTTP1.Request.CustomHeaders.AddValue('Authorization', 'BASIC ' +token);

  try
    jsResponse := TStringStream.Create('', TEncoding.UTF8);

    IdHTTP1.Get(url);

    Result := True;
  except
    on E: EIdHTTPProtocolException do
    begin
      ShowMessage(IntToStr(IdHTTP1.ResponseCode) +' ' +IdHTTP1.ResponseText);
      Result := False;
    end;
  end;
end;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.