Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > Internet
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 03-01-2024
pablog2k pablog2k is offline
Miembro
 
Registrado: may 2017
Posts: 86
Poder: 7
pablog2k Va por buen camino
probablemente te esté pasando que no estás enviando con TLS 1.2 ,que últimamente muchas APIS lo exigen.
Deberás usar los indy mas o menos actualizados, y con el componente TidSSLIOHandlerSocketOpenSSL decirle en sus SSLOptions que la SSLVersion es TLSv1_2
Responder Con Cita
  #2  
Antiguo 03-01-2024
Badillo Badillo is offline
Miembro
 
Registrado: jun 2021
Posts: 33
Poder: 0
Badillo Va por buen camino
Cita:
Empezado por pablog2k Ver Mensaje
probablemente te esté pasando que no estás enviando con TLS 1.2 ,que últimamente muchas APIS lo exigen.
Deberás usar los indy mas o menos actualizados, y con el componente TidSSLIOHandlerSocketOpenSSL decirle en sus SSLOptions que la SSLVersion es TLSv1_2
Gracias, por la idea Pablog2k, probaré y comento.
Responder Con Cita
  #3  
Antiguo 03-01-2024
Badillo Badillo is offline
Miembro
 
Registrado: jun 2021
Posts: 33
Poder: 0
Badillo Va por buen camino
Probé de esta otra forma y mismo error.

Saludos.

Probé de está otra forma, incluyendo lo que me comenta pablog2k y como resultado obtengo el mismo error:

Código Delphi [-]
function TValidateNPIForAPI.ValidateNPIForAPI(aNumber, aNpiType,
  aTaxonomy: string): boolean;
var
  AHTTP: TIdHTTP;
  SSL: TIdSSLIOHandlerSocketOpenSSL;
  xResponse: TStringStream;
  xUrl, xParams: String;
  xJsonObj: TlkJSONbase;
  xCount: Integer;
begin
  Result := False;
  xUrl :=  'https://npiregistry.cms.hhs.gov/api/?version=2.1';
  xResponse := TStringStream.Create('');
  xJsonObj :=  TlkJSONbase.Create;

  if aNumber <> '' then
    xParams := xParams + '&number='+HttpEncode(aNumber);

  if aNpiType <> '' then
    xParams := xParams + '&enumeration_type='+HttpEncode(aNpiType);

  if aTaxonomy <> '' then
    xParams := xParams + '&taxonomy_description='+HttpEncode(aTaxonomy);

  try
    AHTTP:= TIdHTTP.Create(nil);
    SSL:= TIdSSLIOHandlerSocketOpenSSL.Create(nil);

    AHTTP.IOHandler:= SSL;
    AHTTP.AllowCookies:= True;
    AHTTP.HTTPOptions:= [hoForceEncodeParams];

    AHTTP.Request.BasicAuthentication:= True;

    AHTTP.Request.Accept:= 'application/json';
    AHTTP.Request.AcceptCharSet := 'utf-8';
    AHTTP.Request.ContentType:= 'application/json';
    AHTTP.Request.UserAgent:= 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0';
    AHTTP.Request.CharSet:= 'utf-8';

    SSL.SSLOptions.Method:= sslvSSLv23;
    SSL.SSLOptions.Mode:= sslmUnassigned;
    SSL.SSLOptions.VerifyMode:= [];
    SSL.SSLOptions.VerifyDepth:= 0;
    SSL.SSLOptions.SSLVersions := [sslvTLSv1_2, sslvTLSv1_1, sslvSSLv23, sslvTLSv1];

    xParams := AHTTP.Get(xUrl + xParams);
    TlkJSONbase(xJsonObj) := TlkJSON.ParseText(xResponse.DataString);
    xCount := StrToInt(xJsonObj.Field['result_count'].Value);
    Result := (xCount > 0);
  finally
    xResponse.Free;
    xJsonObj.Free;
    SSL.Free;
    AHTTP.Free;
    Result := False;
  end;
end;

Gracias.
Responder Con Cita
  #4  
Antiguo 03-01-2024
pablog2k pablog2k is offline
Miembro
 
Registrado: may 2017
Posts: 86
Poder: 7
pablog2k Va por buen camino
pero tienes que forzar a que envíe por tls 1.2
cambia esto

SSL.SSLOptions.Method:= sslvTLSv1_2;
SSL.SSLOptions.Mode:= sslmUnassigned; (yo suelo poner sslmBoth y me funciona)
SSL.SSLOptions.VerifyMode:= [];
SSL.SSLOptions.VerifyDepth:= 0;
SSL.SSLOptions.SSLVersions := [sslvTLSv1_2];
Responder Con Cita
  #5  
Antiguo 03-01-2024
Badillo Badillo is offline
Miembro
 
Registrado: jun 2021
Posts: 33
Poder: 0
Badillo Va por buen camino
Comparto una vía de solución.

Saludos,

Compartiré la vía con la que solucioné el problema de la llamada a la API.

Código Delphi [-]
unit MdNPI;

interface

uses
  System.SysUtils, System.Variants, System.Classes, System.UITypes,
  System.AnsiStrings, System.IOUtils, uLkJSON;

type

  TNPI = class(TObject)
  private
    { Private declarations }
    FBaseURL: string;
    FParams: string;
    FResponse: string;
    FResponseJson: TlkJSONbase;

    function CallAPI(aUrl: string): string;
  public
    { Public declarations }
    property BaseURL: string read FBaseURL write FBaseURL;
    property ResponseJson: TlkJSONbase read FResponseJson;

    constructor Create;
    function ValidateNPIForAPI(aNumber, aNpiType, aTaxonomy :string): boolean;
  end;

implementation

uses
  System.Character, Vcl.AxCtrls, Winapi.ActiveX, WinHttp_TLB, IdURI;

{ TNPI }

constructor TNPI.Create;
begin
  FBaseURL :=  'https://npiregistry.cms.hhs.gov/api/?version=2.1';
  FParams := '';
  FResponse := '';
  FResponseJson := TlkJSONbase.Create;
end;

function TNPI.CallAPI(aUrl: string): string;
var
  Http: IWinHttpRequest;
  HttpStringStream: TStringStream;
  HttpStream: IStream;
  OleStream: TOleStream;
begin
  Http := CoWinHttpRequest.Create;
  Http.Open('GET', aUrl, False);
  Http.Send(EmptyParam);

  if Http.Status = 200 then
  begin
    HttpStream := IUnknown(Http.ResponseStream) as IStream;
    OleStream  := TOleStream.Create(HttpStream);
    try
      HttpStringStream := TStringStream.Create('');
      try
        OleStream.Position:= 0;
        HttpStringStream.CopyFrom(OleStream, OleStream.Size);

        Result := HttpStringStream.DataString;
      finally
        HttpStringStream.Free;
      end;
    finally
      OleStream.Free;
    end;
  end;
end;

function TNPI.ValidateNPIForAPI(aNumber, aNpiType,
  aTaxonomy: string): boolean;
var
  xCount: Integer;
  xUrl: string;
begin
  try
    xCount := 0;

    if aNumber <> '' then
      FParams := FParams + '&number='+aNumber;

    if aNpiType <> '' then
      FParams := FParams + '&enumeration_type='+aNpiType;

    if aTaxonomy <> '' then
      FParams := FParams + '&taxonomy_description='+aTaxonomy;

    xUrl := TIdURI.URLEncode(FBaseURL + FParams);
    FResponse := CallAPI(xUrl);
    TlkJSONbase(FResponseJson) := TlkJSON.ParseText(FResponse);

    xCount := StrToInt(FResponseJson.Field['result_count'].Value);
  except
    on e: Exception do
      begin
        xCount := 0;
      end;
  end;

  Result := (xCount > 0);

end;

end.

Gracias.
Responder Con Cita
  #6  
Antiguo Hace 4 Semanas
Greender Greender is offline
Registrado
 
Registrado: oct 2022
Posts: 1
Poder: 0
Greender Va por buen camino
Para invocar un API REST con headers, primero debes asegurarte de tener la URL del API y los headers necesarios
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Incluir Headers de SOAP en web services Delphi 2010 Revow Delphi para la web 6 19-05-2015 23:32:59
invocar a una dll lestad Varios 3 07-03-2014 18:47:54
Como modificar los headers de un DBGrid yn4v4s OOP 3 16-07-2012 20:43:07
dbgrid con sub-headers samantha jones Varios 1 02-03-2005 21:30:25
Indicador de orden en los headers de un TListView walrus OOP 1 11-10-2004 19:50:30


La franja horaria es GMT +2. Ahora son las 21:04:14.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi