I have a System.IO.Stream object where I store a HttpContext.Current.Request.InputStream. I need to write the stream to a log file and then process the stream later in my program. It seems that I can only read the stream once and then it disappears.
I've tried a number of copy methods including the CopyTo method, but the second stream never populates.
If I do this:
Dim input As Stream = HttpContext.Current.Request.InputStream
Dim tmpSR As StreamReader = New StreamReader(input)
Response.Write(tmpSR.ReadToEnd())
The stream outputs just fine, but then I can't access the stream again which leads me to believe that after its read, it's gone.
I have tried this:
Dim logfile As String = "mylogfile_" & DateTime.Now.ToString("yyyyMMdd") & ".log"
Dim objFileStream As FileStream
Dim asyncResult As IAsyncResult
Dim writeBuffer As Byte()
Dim logText As String
Dim input As Stream = HttpContext.Current.Request.InputStream
Dim inputSave As Stream = input
Dim tmpSR As StreamReader = New StreamReader(input)
Dim tmpSR2 As StreamReader = New StreamReader(inputSave)
logText = (DateTime.Now.ToString("yyyyMMdd HHmmss")) & "Request Method: " & Request.RequestType & " "
objFileStream = New FileStream(logfile,FileMode.Append,FileAccess.Write,Fileshare.ReadWrite,4096,True)
Using objStreamWriter As StreamWriter = New StreamWriter(objFileStream)
writeBuffer = Encoding.ASCII.GetBytes(logText)
asyncResult = objFileStream.BeginWrite(writeBuffer,0,writeBuffer.Length,Nothing,Nothing)
objStreamWriter.WriteLine()
objStreamWriter.Write(tmpSR.ReadToEnd())
objStreamWriter.WriteLine()
objFileStream.EndWrite(asyncResult)
objFileStream.Flush()
objStreamWriter.Flush()
objStreamWriter.Close()
End Using
objFileStream.Close()
Response.Write(tmpSR2.ReadToEnd())
The log file receives the Stream data; however, the 'saved' Stream that I want to Write out to the browser returns 0 bytes. It appears to have vanished.
I've read a bit about the CopyTo() method of the Stream class that's available in 4.0, but I haven't been able to implement that method with any success either.
As I have said, I'm pretty new to ASP.net. I'm using VB and my framework is 4.0. Any guidance would be appreciated.
I just figured out what I needed to do. It was so simple in the end.
I kept thinking that the stream was disappearing and that I had to copy it off somewhere in order to read it again. In the end, all that I needed to do was reset the position of the stream to 0. For anyone else that needs this...
Dim logfile As String = "mylogfile_" & DateTime.Now.ToString("yyyyMMdd") & ".log"
Dim objFileStream As FileStream
Dim asyncResult As IAsyncResult
Dim writeBuffer As Byte()
Dim logText As String
Dim input As Stream = HttpContext.Current.Request.InputStream
Dim inputSR As StreamReader = New StreamReader(input)
logText = (DateTime.Now.ToString("yyyyMMdd HHmmss")) & " Request Method: " & Request.RequestType & " "
objFileStream = New FileStream(logfile,FileMode.Append,FileAccess.Write,Fileshare.ReadWrite,4096,True)
Using objStreamWriter As StreamWriter = New StreamWriter(objFileStream)
writeBuffer = Encoding.ASCII.GetBytes(logText)
asyncResult = objFileStream.BeginWrite(writeBuffer,0,writeBuffer.Length,Nothing,Nothing)
objStreamWriter.WriteLine()
objStreamWriter.Write(inputSR.ReadToEnd())
objStreamWriter.WriteLine()
objFileStream.EndWrite(asyncResult)
objFileStream.Flush()
objStreamWriter.Flush()
objStreamWriter.Close()
End Using
objFileStream.Close()
input.Position = 0
Response.Write(inputSR.ReadToEnd())
I hope this helps some other newbie!
Marked as answer by compbrat75 on Dec 13, 2012 09:03 PM
compbrat75
0 Points
2 Posts
Newbie Stream Behavior Question
Dec 04, 2012 08:48 PM|LINK
Can a Stream only be read once?
I have a System.IO.Stream object where I store a HttpContext.Current.Request.InputStream. I need to write the stream to a log file and then process the stream later in my program. It seems that I can only read the stream once and then it disappears.
I've tried a number of copy methods including the CopyTo method, but the second stream never populates.
If I do this:
The stream outputs just fine, but then I can't access the stream again which leads me to believe that after its read, it's gone.
I have tried this:
Dim logfile As String = "mylogfile_" & DateTime.Now.ToString("yyyyMMdd") & ".log" Dim objFileStream As FileStream Dim asyncResult As IAsyncResult Dim writeBuffer As Byte() Dim logText As String Dim input As Stream = HttpContext.Current.Request.InputStream Dim inputSave As Stream = input Dim tmpSR As StreamReader = New StreamReader(input) Dim tmpSR2 As StreamReader = New StreamReader(inputSave) logText = (DateTime.Now.ToString("yyyyMMdd HHmmss")) & "Request Method: " & Request.RequestType & " " objFileStream = New FileStream(logfile,FileMode.Append,FileAccess.Write,Fileshare.ReadWrite,4096,True) Using objStreamWriter As StreamWriter = New StreamWriter(objFileStream) writeBuffer = Encoding.ASCII.GetBytes(logText) asyncResult = objFileStream.BeginWrite(writeBuffer,0,writeBuffer.Length,Nothing,Nothing) objStreamWriter.WriteLine() objStreamWriter.Write(tmpSR.ReadToEnd()) objStreamWriter.WriteLine() objFileStream.EndWrite(asyncResult) objFileStream.Flush() objStreamWriter.Flush() objStreamWriter.Close() End Using objFileStream.Close() Response.Write(tmpSR2.ReadToEnd())The log file receives the Stream data; however, the 'saved' Stream that I want to Write out to the browser returns 0 bytes. It appears to have vanished.
I've read a bit about the CopyTo() method of the Stream class that's available in 4.0, but I haven't been able to implement that method with any success either.
As I have said, I'm pretty new to ASP.net. I'm using VB and my framework is 4.0. Any guidance would be appreciated.
Thanks!
~Maria
Angie xu - M...
All-Star
18045 Points
1550 Posts
Microsoft
Re: Newbie Stream Behavior Question
Dec 11, 2012 06:07 AM|LINK
Hi compbrat
Yes, you could use Stream.CopyTo Method to read the bytes from the current stream and write them to the destination stream.
But StreamReader is designed for character input in a particular encoding, use StreamReader for reading lines of information from
a standard text file.
StreamReader is for text data. You will almost certainly lose data if you use it for arbitrary binary data.
You could learn StreamReader here.
kind regards
Feedback to us
Develop and promote your apps in Windows Store
compbrat75
0 Points
2 Posts
Re: Newbie Stream Behavior Question
Dec 13, 2012 09:03 PM|LINK
Hi Angie,
Thank you for the response!
I just figured out what I needed to do. It was so simple in the end.
I kept thinking that the stream was disappearing and that I had to copy it off somewhere in order to read it again. In the end, all that I needed to do was reset the position of the stream to 0. For anyone else that needs this...
Dim logfile As String = "mylogfile_" & DateTime.Now.ToString("yyyyMMdd") & ".log" Dim objFileStream As FileStream Dim asyncResult As IAsyncResult Dim writeBuffer As Byte() Dim logText As String Dim input As Stream = HttpContext.Current.Request.InputStream Dim inputSR As StreamReader = New StreamReader(input) logText = (DateTime.Now.ToString("yyyyMMdd HHmmss")) & " Request Method: " & Request.RequestType & " " objFileStream = New FileStream(logfile,FileMode.Append,FileAccess.Write,Fileshare.ReadWrite,4096,True) Using objStreamWriter As StreamWriter = New StreamWriter(objFileStream) writeBuffer = Encoding.ASCII.GetBytes(logText) asyncResult = objFileStream.BeginWrite(writeBuffer,0,writeBuffer.Length,Nothing,Nothing) objStreamWriter.WriteLine() objStreamWriter.Write(inputSR.ReadToEnd()) objStreamWriter.WriteLine() objFileStream.EndWrite(asyncResult) objFileStream.Flush() objStreamWriter.Flush() objStreamWriter.Close() End Using objFileStream.Close() input.Position = 0 Response.Write(inputSR.ReadToEnd())I hope this helps some other newbie!