Rewrite the code to use the secureBridge

Discussion of open issues, suggestions and bugs regarding network security and data protection solution - SecureBridge
Post Reply
alexandrenicolas
Posts: 7
Joined: Fri 15 Mar 2019 14:12

Rewrite the code to use the secureBridge

Post by alexandrenicolas » Tue 19 Mar 2019 11:57

Hello,

I've bought the SecureBridge so that I could use Https in my application.

I am having difficulties to rewrite a small code where I could use the secureBridge, I am not sure the steps and if the correct component would it be the ScHttpWebRequest, I appreciate a lot your help with my small code:

Retry:= True;
Tries := 0;
while Retry do
begin
HTTPSend.Clear;
PostStr := 'cnpj='+OnlyNumber(ACNPJ)+'&' +
'origem=comprovante&' +
'search_type=cnpj&' +
'submit1=Consultar&' +
'txtTexto_captcha_serpro_gov_br='+Trim(ACaptcha);

WriteStrToStream( HTTPSend.Document, PostStr );
HTTPSend.MimeType := 'application/x-www-form-urlencoded';
HTTPSend.Cookies.Add('flag=1');
HTTPSend.Headers.Add('Referer: https://www.receita.fazenda.gov.br/pess ... tacao3.asp');
HTTPPost('https://www.receita.fazenda.gov.br/pess ... valida.asp');

Retry := (Tries < 2) and
(pos('Captcha Sonoro', RespHTTP.Text) > 0) and
(pos(ACBrStr('Digite o número de CNPJ da empresa e clique em'), RespHTTP.Text) > 0);
Inc( Tries );
end;

regards
Alexandre

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Rewrite the code to use the secureBridge

Post by ViktorV » Wed 20 Mar 2019 14:33

You can try to use next code:

Code: Select all

var
  Request: TScHttpWebRequest;
  Response: TScHttpWebResponse;
  Buf: TBytes;
  PostStr, RespStr: string;
...
  Request := TScHttpWebRequest.Create('https://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/valida.asp');
  Request.Referer := 'https://www.receita.fazenda.gov.br/pessoajuridica/cnpj/cnpjreva/Cnpjreva_solicitacao3.asp';
  Request.Cookies.Add('flag=1');

  Request.Method := rmPost;
  PostStr := 'cnpj='+OnlyNumber(ACNPJ)+'&' +
    'origem=comprovante&' +
    'search_type=cnpj&' +
    'submit1=Consultar&' +
    'txtTexto_captcha_serpro_gov_br='+Trim(ACaptcha);
  Buf := TEncoding.UTF8.GetBytes(PostStr);

  try
    Request.ContentType := 'application/x-www-form-urlencoded';
    Request.ContentLength := Length(Buf);
    Request.WriteBuffer(Buf);
    Response := nil;
    try
      Response := Request.GetResponse;
      RespStr := Response.ReadAsString;
    finally
      if Response <> nil then
        Response.Free;
    end;
  finally
    Request.Free;
  end;

alexandrenicolas
Posts: 7
Joined: Fri 15 Mar 2019 14:12

Re: Rewrite the code to use the secureBridge

Post by alexandrenicolas » Thu 21 Mar 2019 11:20

Hello ViktorV,

First of all, I want to thank you for your help.

In your code there is function TEncoding.UTF8.GetBytes(PostStr) used,
where can I get this functin TEncoding.UTF8.GetBytes(PostStr) ? It seems there isn't it for delphi 2007.


regards
Alex

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Rewrite the code to use the secureBridge

Post by ViktorV » Fri 22 Mar 2019 14:56

Please try ti use the next code for Dephi 2007:

Code: Select all

  SetLength(Buf, Length(PostStr));
  Move(PAnsiChar(PostStr)^, Pointer(Buf)^, Length(PostStr));

Post Reply