TScHttpWebResponse ReadAsBytes
-
- Posts: 9
- Joined: Fri 20 Jul 2018 16:45
TScHttpWebResponse ReadAsBytes
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;
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
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;
-
- Posts: 9
- Joined: Fri 20 Jul 2018 16:45
Re: TScHttpWebResponse ReadAsBytes
Thank you so much. I will test it.
-
- Posts: 9
- Joined: Fri 20 Jul 2018 16:45
Re: TScHttpWebResponse ReadAsBytes
It Works Great!
Thanks
Thanks
Re: TScHttpWebResponse ReadAsBytes
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.
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.
-
- Posts: 7
- Joined: Fri 15 Mar 2019 14:12
Re: TScHttpWebResponse ReadAsBytes
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
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
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;
-
- Posts: 7
- Joined: Fri 15 Mar 2019 14:12
Re: TScHttpWebResponse ReadAsBytes
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
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
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.
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.
-
- Posts: 7
- Joined: Fri 15 Mar 2019 14:12
Re: TScHttpWebResponse ReadAsBytes
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?
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
To solve the issue you can use the next code:
Code: Select all
Stream.Write(buf[0], Length(buf));
-
- Posts: 7
- Joined: Fri 15 Mar 2019 14:12
Re: TScHttpWebResponse ReadAsBytes
Thanks very much for your help, it solved the problem
Re: TScHttpWebResponse ReadAsBytes
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.
Feel free to contact us if you have any further questions about our products.