egnew 3 Posted June 27, 2023 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
aehimself 396 Posted June 27, 2023 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. 1 Share this post Link to post
Dave Nottage 557 Posted June 27, 2023 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 ;-) 1 1 Share this post Link to post
egnew 3 Posted June 28, 2023 I typecast the ContentStream as a TMemoryStream and SaveToFile worked perfectly. Thanks for your help. 1 Share this post Link to post
aehimself 396 Posted June 28, 2023 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