TScHttpWebResponse ReadAsBytes

Discussion of open issues, suggestions and bugs regarding network security and data protection solution - SecureBridge
Post Reply
byronrocha
Posts: 9
Joined: Fri 20 Jul 2018 16:45

TScHttpWebResponse ReadAsBytes

Post by byronrocha » Tue 12 Mar 2019 21:53

Hi

Could you share an example how to use the Method ReadAsBytes? I am trying to get an image using a GET. So I am working with the TScHttpWebRequest and TScHttpWebResponse;

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

Re: TScHttpWebResponse ReadAsBytes

Post by ViktorV » Thu 14 Mar 2019 14:47

To solve the task, you can use the following code:

Code: Select all

var
  Image: Timage;
...
var
  Request: TScHttpWebRequest;
  Response: TScHttpWebResponse;
  Buf: TBytes;
  Stream: TMemoryStream;
  JPG: TJPEGImage;
begin
  JPG := TJPEGImage.Create;
  Stream := TMemoryStream.Create;
  Request := TScHttpWebRequest.Create('https://community.embarcadero.com/uploads/6168/InstallerNew.jpg');
  try
    Response := Request.GetResponse;
    try
      Buf := Response.ReadAsBytes;
      Stream.Write(buf, Length(buf));
      Stream.Position := 0;
      JPG.LoadFromStream(Stream);
      Image.Picture.Graphic := JPG;
    finally
      Response.Free;
    end;
  finally
    Request.Free;
    Stream.Free;
    JPG.Free;
  end;
end;  

byronrocha
Posts: 9
Joined: Fri 20 Jul 2018 16:45

Re: TScHttpWebResponse ReadAsBytes

Post by byronrocha » Thu 14 Mar 2019 16:56

Thank you so much. I will test it.

byronrocha
Posts: 9
Joined: Fri 20 Jul 2018 16:45

Re: TScHttpWebResponse ReadAsBytes

Post by byronrocha » Thu 14 Mar 2019 22:32

It Works Great!
Thanks

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

Re: TScHttpWebResponse ReadAsBytes

Post by ViktorV » Fri 15 Mar 2019 14:09

Thank you for the interest to our product.
If you have any questions during using our products, please don't hesitate to contact us - and we will try to help you solve them.

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

Re: TScHttpWebResponse ReadAsBytes

Post by alexandrenicolas » Fri 15 Mar 2019 16:54

Hello,

I have accompany this thread and I am very interested on it.

I have tried the code but I am getting an error, please take a look at the code:

procedure TF_Principal.Captcha2();
var
Request: TScHttpWebRequest;
Response: TScHttpWebResponse;
Buf: TBytes;
Stream: TMemoryStream;
Png : TPNGGraphic;

Request := TScHttpWebRequest.Create('https://www.receita.fazenda.gov.br/pess ... aptcha.asp');
try
Response := Request.GetResponse;
try
Stream:= TMemoryStream.Create;
Png := TPNGGraphic.create;
buf := Response.ReadAsBytes;
Stream.Write(buf, Length(buf));
Stream.Position := 0;
png.LoadFromStream( Stream );
sImage1.Picture.Assign(Png);

finally
Response.Free;
end;
finally
Request.Free;
Stream.Free;

end;
end;

I've also tried your example with this URL:
Request := TScHttpWebRequest.Create('https://community.embarcadero.com/uploa ... lerNew.jpg');
with the code above but I got the same error:

"Access violation at address 004031E3 in module 'ConsultaCNPJ.exe'. Read of address 00197D5C."

this error happens in this line:

Stream.Write(buf, Length(buf));

What could it be?
thanks a lot
regards
Alexandre / Brazil

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

Re: TScHttpWebResponse ReadAsBytes

Post by ViktorV » Mon 18 Mar 2019 07:20

This code may cause an error if buf = nil. To solve the problem, you can use the following code:

Code: Select all

if Buf <> nil then begin
  Stream.Write(buf, Length(Buf));
  Stream.Position := 0;
  Png.LoadFromStream(Stream);
  Image.Picture.Assign(Png);
end;

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

Re: TScHttpWebResponse ReadAsBytes

Post by alexandrenicolas » Mon 18 Mar 2019 12:45

Hello ViktorV,

I don't have my buf = nil, but I am having problem in this line:

Stream.Write(buf, Length(buf));

Please, try this code and see if you can load a jpeg into an img component.

procedure TF_Principal.Captcha3();
var
Request: TScHttpWebRequest;
Response: TScHttpWebResponse;
Buf: TBytes;
Stream: TMemoryStream;
JPG: TJPEGImage;
begin
JPG := TJPEGImage.Create;
Stream := TMemoryStream.Create;
Request := TScHttpWebRequest.Create('https://community.embarcadero.com/uploa ... lerNew.jpg');
try
Response := Request.GetResponse;
try
Buf := Response.ReadAsBytes;
if Buf<>nil then
Begin
Stream.Write(buf, Length(buf));
Stream.Position := 0;
JPG.LoadFromStream(Stream);
sImage1.Picture.Graphic := JPG;
End;
finally
Response.Free;
end;
finally
Request.Free;
Stream.Free;
JPG.Free;
end;
end;




Thanks
Alex

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

Re: TScHttpWebResponse ReadAsBytes

Post by ViktorV » Mon 18 Mar 2019 13:21

Unfortunately, we could not reproduce the issue.
When executing the codes you specified, we did not receive the error you indicated. Please make sure that you are using the latest version of SecureBridge 9.0.2 and indicate which version of IDE you are using.

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

Re: TScHttpWebResponse ReadAsBytes

Post by alexandrenicolas » Mon 18 Mar 2019 14:58

Hi,

I am using the latest version 9.02.

The version of my delphi is 2007, do you think that I am getting this error because of my delphi version?

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

Re: TScHttpWebResponse ReadAsBytes

Post by ViktorV » Tue 19 Mar 2019 08:16

To solve the issue you can use the next code:

Code: Select all

  Stream.Write(buf[0], Length(buf));

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

Re: TScHttpWebResponse ReadAsBytes

Post by alexandrenicolas » Tue 19 Mar 2019 11:49

Thanks very much for your help, it solved the problem

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

Re: TScHttpWebResponse ReadAsBytes

Post by ViktorV » Wed 20 Mar 2019 14:33

It is good to see that the problem has been solved.
Feel free to contact us if you have any further questions about our products.

Post Reply