How to do autentica basic with delphi xe 8

1

I'm developing an application in Delphi xe8 that will consume data from the web service that returns data in json. The problem is that you are presenting the message

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

How do I send this authorization of the type basic.

Thanks in advance for your attention.

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;
    
asked by anonymous 11.05.2018 / 18:55

1 answer

0

Responding to the main question, to send a BASIC authentication using Indy , you can use a function similar to the 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;
    
24.05.2018 / 20:18