Jump to content
egnew

TNetHttpRequest.ContentStream Save To File

Recommended Posts

I am using a TNetHttpRequest component to retrieve text from a secure website.  I use the associated TNetHttpClient's OnAuthEvent to set the username and password.  This allows me to retrieve text into a string using IHTTPResponse.ContentasString.

 

I need to retrieve files from the same secured website.  When I request the file using the same code, the IHTTPResponse.ContentStream seems to contain the requested file content since the IHTTPResponse.ContentLength appears to be correct.

 

How can I save the content of IHTTPResponse.ContentStream to a file?

 

Thanks

Share this post


Link to post
3 hours ago, egnew said:

I am using a TNetHttpRequest component to retrieve text from a secure website.  I use the associated TNetHttpClient's OnAuthEvent to set the username and password.  This allows me to retrieve text into a string using IHTTPResponse.ContentasString.

 

I need to retrieve files from the same secured website.  When I request the file using the same code, the IHTTPResponse.ContentStream seems to contain the requested file content since the IHTTPResponse.ContentLength appears to be correct.

 

How can I save the content of IHTTPResponse.ContentStream to a file?

 

Thanks

AFFAIK it's a TMemoryStream, so you simply can call ContentStream.SaveToFile. You also can create a separate TFileStream and use .CopyFrom(ContentStream). just make sure position is 0 before calling .CopyFrom.

  • Like 1

Share this post


Link to post
3 minutes ago, aehimself said:

so you simply can call ContentStream.SaveToFile

You'd need to typecast it to a TMemoryStream in order to do that, e.g:

if LResponse.ContentStream is TMemoryStream then
  TMemoryStream(LResponse.ContentStream).SaveToFile(LFileName)
// else the implementation has changed ;-)

 

  • Like 1
  • Thanks 1

Share this post


Link to post

I typecast the ContentStream as a TMemoryStream and SaveToFile worked perfectly.  Thanks for your help.

  • Like 1

Share this post


Link to post
20 hours ago, Dave Nottage said:

You'd need to typecast it to a TMemoryStream in order to do that, e.g:


if LResponse.ContentStream is TMemoryStream then
  TMemoryStream(LResponse.ContentStream).SaveToFile(LFileName)
// else the implementation has changed ;-)

 

Yes, it is exposed as TStream, but internally it's a TMemoryStream if I recall correctly. So yes, typecasting is needed.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×