Page 1 of 1

TScHttpWebResponse ReadAsBytes

Posted: Tue 12 Mar 2019 21:53
by byronrocha
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;

Re: TScHttpWebResponse ReadAsBytes

Posted: Thu 14 Mar 2019 14:47
by ViktorV
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;  

Re: TScHttpWebResponse ReadAsBytes

Posted: Thu 14 Mar 2019 16:56
by byronrocha
Thank you so much. I will test it.

Re: TScHttpWebResponse ReadAsBytes

Posted: Thu 14 Mar 2019 22:32
by byronrocha
It Works Great!
Thanks

Re: TScHttpWebResponse ReadAsBytes

Posted: Fri 15 Mar 2019 14:09
by ViktorV
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.

Re: TScHttpWebResponse ReadAsBytes

Posted: Fri 15 Mar 2019 16:54
by alexandrenicolas
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

Re: TScHttpWebResponse ReadAsBytes

Posted: Mon 18 Mar 2019 07:20
by ViktorV
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;

Re: TScHttpWebResponse ReadAsBytes

Posted: Mon 18 Mar 2019 12:45
by alexandrenicolas
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

Re: TScHttpWebResponse ReadAsBytes

Posted: Mon 18 Mar 2019 13:21
by ViktorV
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.

Re: TScHttpWebResponse ReadAsBytes

Posted: Mon 18 Mar 2019 14:58
by alexandrenicolas
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?

Re: TScHttpWebResponse ReadAsBytes

Posted: Tue 19 Mar 2019 08:16
by ViktorV
To solve the issue you can use the next code:

Code: Select all

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

Re: TScHttpWebResponse ReadAsBytes

Posted: Tue 19 Mar 2019 11:49
by alexandrenicolas
Thanks very much for your help, it solved the problem

Re: TScHttpWebResponse ReadAsBytes

Posted: Wed 20 Mar 2019 14:33
by ViktorV
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.