unit EnviaHttp;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, DateUtils, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdHTTP, IdIOHandler, IdIOHandlerSocket,
  IdIOHandlerStack, IdSSL, IdSSLOpenSSL;

type
  TForm4 = class(TForm)
    edtUser: TLabeledEdit;
    edtSenha: TLabeledEdit;
    edtToken: TLabeledEdit;
    mmoXML: TMemo;
    lbl1: TLabel;
    mmoRetorno: TMemo;
    lbl2: TLabel;
    btnEnviar: TButton;
    btnArquivo: TButton;
    OpenDialog: TOpenDialog;
    procedure FormCreate(Sender: TObject);
    procedure btnEnviarClick(Sender: TObject);
    procedure btnArquivoClick(Sender: TObject);
  private
    { Private declarations }
      fHTTPClient : TIdHTTP;
      fIOHandlerSocketOpenSSL : TIdSSLIOHandlerSocketOpenSSL;
      fParamList : TStringList;
      property HTTPClient : TIdHTTP read fHTTPClient write fHTTPClient;
      property ParamList : TStringList read fParamList write fParamList;
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}

procedure TForm4.btnArquivoClick(Sender: TObject);
var
 _xml : TStringStream;
 _file : string;
begin
_xml := TStringStream.Create(nil);
if OpenDialog.Execute then
begin
  _file := OpenDialog.FileName;
end;
 _xml.LoadFromFile(_file);
 mmoXML.Text := _xml.DataString;
 FreeAndNil(_xml);
end;

procedure TForm4.btnEnviarClick(Sender: TObject);
var
 url, user, senha, token, XML : string;
begin
  url := 'https://app.notasegura.com.br/api/invoices';
  user := edtUser.Text;
  senha := edtSenha.Text;
  token := edtToken.Text;
  XML := mmoXML.Text;
  HTTPClient.Request.Accept := 'application/xml';
  HTTPClient.Request.ContentType := 'application/x-www-form-urlencoded';
  HTTPClient.Request.BasicAuthentication := True;
  HTTPClient.Request.Username := edtUser.Text;
  HTTPClient.Request.Password := edtSenha.Text;
  ParamList.Values['token'] := token;
  ParamList.Values['xml'] := XML;
  mmoRetorno.Text :=   fHTTPClient.Post(url, ParamList);
end;

procedure TForm4.FormCreate(Sender: TObject);
begin
  fHTTPClient := TIdHTTP.Create(nil);
  fIOHandlerSocketOpenSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  fParamList := TStringList.Create;
  fParamList.NameValueSeparator := '=';
  HTTPClient.ProtocolVersion := pv1_1;
  HTTPClient.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko';
  fHTTPClient.IOHandler := fIOHandlerSocketOpenSSL;

end;


end.
