Ver Mensaje Individual
  #15  
Antiguo 04-08-2016
fcios fcios is offline
Miembro
 
Registrado: jul 2003
Posts: 108
Reputación: 21
fcios Va por buen camino
Hola yo lo hice con 2 componentes de las ICS

TSslHttpCli y TSslContext;

// Consulta a Padrón Nivel 3 WSPN3

Código Delphi [-]
unit Unit1;
// usa los ICS de OverbyteIcsV7Gold
// http://www.overbyte.be/frame_index.h...ducts/ics.html

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OverbyteIcsWSocket, OverbyteIcsWndControl, OverbyteIcsHttpProt,
  StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    SslHttpCli1: TSslHttpCli;
    SslContext1: TSslContext;
    Label5: TLabel;
    ConsultarCuitBB: TBitBtn;
    ICSMemo1M: TMemo;
    Memo1: TMemo;
    SalirBB: TBitBtn;
    ConsultarCuitE: TEdit;
    procedure SslHttpCli1RequestDone(Sender: TObject; RqType: THttpRequest;
      ErrCode: Word);
    procedure ConsultarCuitBBClick(Sender: TObject);
    procedure SalirBBClick(Sender: TObject);
  private
    { Private declarations }
    sAux : string;
    function  BuscoValorDelNodo1(const sTexto:{wide}String; const sNodoABuscar:string; var sRespuesta:{wide}string):boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses StrUtils{PosEx};

const
  myEOL        = #13+#10; // simula enter o fin de linea

{$R *.dfm}

procedure TForm1.ConsultarCuitBBClick(Sender: TObject);
var
  nCantErrores :byte;
  sCuit : string;
  sRespu :string;
const nMAX_ERRORES = 2; // PORQUE LA PRIMERA VEZ QUE SE CONECTA ES POSIBLE QUE DE ERROR

begin
  ConsultarCuitBB.Enabled := False;
  ConsultarCuitBB.Update;        

  ConsultarCuitE.Text := StringReplace(ConsultarcuitE.Text,'-','',[rfReplaceAll]);
  sCuit := ConsultarCuitE.Text;

  SslHttpCli1.URL := 'https://soa.afip.gob.ar/sr-padron/v2/persona/'+
    sCuit;

  SslHttpCli1.RcvdStream := TMemoryStream.Create;

  nCantErrores :=0;
  while True do begin
    try
      IcSMemo1M.Lines.Clear;
      SslHttpCli1.Get;
    except
      on E: Exception do begin
        if e.Message = 'Connection aborted on request' then begin
          ICSMemo1M.Lines.Append('Error al conectar, verifique tener libeay32.dll y ssleay32.dll'+myeol+
                              e.Message {+' '+e.ClassName});
          ConsultarCuitBB.Enabled := True;
          Exit;
        end;
        ICSMemo1M.Lines.Append(myEOL+'Error al conectar '+myeol+e.Message {+' '+e.ClassName});
        ConsultarCuitBB.Enabled := True;
        exit;
      end;
    end;        // 123,34,115,117, = {"su

//  if SslHttpCli1.RcvdStream = nil then begin
    if SslHttpCli1.RcvdCount = 0 then begin
        Inc(nCantErrores);
        sAux := DateTimeToStr(Now) + ' ERROR DE CONEXION, RX, ' +
          ' INTENTO ' + IntToStr(nCantErrores) +myeol+ 'VERIFIQUE QUE EXISTE LIBEAY32.DLL Y SSLEAY32.DLL' ;
          // myEOL+ e.ClassName + myEOL + e.Message;
        //  GraboErrorLog(sSistema+myEOL+sAux,not bMostrarError);
        ICSMemo1M.Lines.Append(sAux);
        if nCantErrores <= nMAX_ERRORES then begin
          // MuestroErrorArba(False,True,sAux);
          ICSMemo1M.Lines.Append('Intento '+intToStr(nCantErrores));
        end else
        begin
          //MuestroErrorArba(False,True,sAux,5);
          ICSMemo1M.Lines.Append(sAux);
          Break;
        end;
      Continue;
    end;

    Break;
  end;
  SslHttpCli1.RcvdStream.Free;
  SslHttpCli1.RcvdStream := nil;

  if BuscoValorDelNodo1(ICSMemo1M.Text,'success', sRespu) = True then begin
    Memo1.Lines.Append('success='+sRespu);
  end else begin
    memo1.Lines.Append('No se encuentra cuit ' + sCuit + myeol+
      ICSMemo1m.Text);
  end;

  if BuscoValorDelNodo1(ICSMemo1M.Text,'idPersona', sRespu) = True then begin
    Memo1.Lines.Append('idPersona='+sRespu);
  end else begin
    memo1.Lines.Append('No se encuentra cuit ' + sCuit + myeol+
      ICSMemo1m.Text);
  end;

  if BuscoValorDelNodo1(ICSMemo1M.Text,'tipoPersona', sRespu) = True then begin
    Memo1.Lines.Append('tipoPersona='+ sRespu);
  end else begin
    memo1.Lines.Append('No se encuentra tipoPersona para ' + sCuit + myeol+
      ICSMemo1m.Text);
  end;


  //
  ConsultarCuitBB.Enabled := True;
end;

function  TForm1.BuscoValorDelNodo1(const sTexto:{wide}String; const sNodoABuscar:string; var sRespuesta:{wide}string):boolean;
// usada para AFIP
// si no encontró el sNodoABuscar retorna False y limpia sRespuesta
// sTexto : texto donde buscar el sNodoABuscar
// sNodoABuscar es el string a buscar ej. 'direccion'
//
var n,n2,nLongNodo:integer;
begin
  n:=Pos('"' + sNodoABuscar + '":',sTexto);
  if n = 0 then begin // si no lo encuentra retorna FALSE
    Result := False;
  sRespuesta := '';
    Exit;
  end;
  Result := True;
  nLongNodo := Length(sNodoABuscar) + 3;

  // busco donde empieza el nodo
  if sTexto[n + nLongNodo ] = '[' then begin // si es array
    n2 := PosEx(']',sTexto,n) +1 ;
  end
  else
    n2 := PosEx(',',sTexto,n);

  if sTexto[n2-1] = '}' then // saco el cierre } de por ejemplo idProvincia
    Dec(n2);


//  if n2 = 0 then
//    MuestroError(True,'NO SE ENCUENTRA FIN DE NODO' + sNodoABuscar,LogSL,True);
//sRespuesta := MidStr(sTexto,n+nLongNodo,n2{-n-nLongNodo});
  sRespuesta := copy(sTexto,n+nLongNodo,n2-n-nLongNodo);
end;


procedure TForm1.SslHttpCli1RequestDone(Sender: TObject;
  RqType: THttpRequest; ErrCode: Word);
var
  Data : String;
begin
    SslHttpCli1.SendStream.Free;
    SslHttpCli1.SendStream := nil;

    if ErrCode <> 0 then begin
        ICSMemo1M{ICSErrorM}.Lines.Append('Post error #' + IntToStr(ErrCode));
        SslHttpCli1.RcvdStream.Free;
        SslHttpCli1.RcvdStream := nil;
        Exit;
    end;
    if SslHttpCli1.StatusCode <> 200 then begin
        ICSMemo1M{ICSErrorM}.Lines.Append('Post error: ' + IntToStr(SslHttpCli1.StatusCode) +
                SslHttpCli1.ReasonPhrase);
        SslHttpCli1.RcvdStream.Free;
        SslHttpCli1.RcvdStream := nil;
        Exit;
    end;
//  Memo1.Lines.Append('Post was OK. Response was:');
    SslHttpCli1.RcvdStream.Seek(0, 0);
    SetLength(Data, SslHttpCli1.RcvdStream.Size);
    SslHttpCli1.RcvdStream.Read(Data[1], Length(Data));
    ICSMemo1M.Lines.Append(Data);
  // grabo log
//  ArbaLogSL.Append(FormatDateTime('dd-mm-yyyy hh:nn:ss ',now)+ UltimaActualizacionL.Caption+
//    ' TX '+ArbaDatosSL[ccDESCRIPCION]+
//    myEOL + ArbaSL.Text +myEOL+
//   'RX'+ myEOL + SslHttpCli1.RcvdHeader.Text +myEOL+ Data +myEOL);

//  ArbaLogSL.SaveToFile(sArbaArchivoLog);
end;

procedure TForm1.SalirBBClick(Sender: TObject);
begin
  Close;
end;

end.

Última edición por fcios fecha: 04-08-2016 a las 01:50:38.
Responder Con Cita