[RESOLVED] Using Seek() method for GetResponseStream()
Hi,
I am using the GetResponseStream.Read() function of the HttpWebResponse to read bytes of a remote file. I was wondering how to seek to a particular position and read from that position. I tried to assign the stream obtained from GetResponseStream() to a FileStream but it seems to be of type ConnectStream and is throwing typecasting errors. My idea is to use the Seek() method.
So, how would we use the Seek() method in this case ?
Thanks in advance..
:wave:
Re: Using Seek() method for GetResponseStream()
You shouldn't. You don't. You can't. As the name suggests, a FileStream is a Stream on a file. HttpWebRequest.GetResponseStream will, if I'm not mistaken, return a NetworkStream, which is a Stream on a network. That makes sense, given that the data is being received over the internet or an intranet. The documentation for the NetworkStream.Seek method says:
Quote:
This method is not currently supported and always throws a NotSupportedException.
It makes sense when you think about it. In the case of a FileStream, the data being streamed is sitting there on the system and always available. Seeking back and forth is no issue because the data doesn't move. In the case of a NetworkStream, data comes in, you read it and it's gone, so you can't seek back and forth willy-nilly. It's basically like a data reader in ADO.NET. If you want to be able to access the data randomly then you would have to read it all in first and then you can navigate back and forth using a local MemoryStream or FileStream. With a NetworkStream though, you must read all the data in serial and once only.
Re: Using Seek() method for GetResponseStream()
Thanks JM :wave:
I was actually reading the docs of Read() method and has first thought it is the seconds parameter(ie, offset) that needs to be changes. Then after reading again found that that the offset of the byte array that it is mentioning.
I then tried to assign the stream from GetResponseStream() to a Stream object. And tried using it. It actually works for the downloading part. But the Seek() method won't work and it throws an error saying that it doesn't support the Seek() method for this stream, like you said.
So what would be your suggestion ? What I was actually trying to do is, downloading of a file as well as pausing it and then resuming it after restarting the app. The downloading part works flawlessly. But the thing that haunts me is the pausing and resuming part.
Any tips or suggestions ?
Re: Using Seek() method for GetResponseStream()
Quote:
Originally Posted by
akhileshbc
Thanks I then tried to assign the stream from GetResponseStream() to a Stream object.
No. You can't assign anything to an object. An object is what gets assigned. GetResponseStream returns a Stream object and you can then assign that object to a variable.
Quote:
Originally Posted by
akhileshbc
So what would be your suggestion ? What I was actually trying to do is, downloading of a file as well as pausing it and then resuming it after restarting the app. The downloading part works flawlessly. But the thing that haunts me is the pausing and resuming part.
There's no need to Seek using the Stream when resuming. When you start a download you get a Stream from a WebResponse and you start calling Read in a loop. You read in blocks, e.g. 4K. When the user pauses you remember how much data you have already downloaded and you close the Stream.
When the user resumes, you call the AddRange method of the HttpWebRequest to specify where to start reading. If you read 10 blocks before the user paused then you would pass (10 * 4096) as the argument to AddRange to specify that you wanted to read the range of data from that point to the end of the file. You then do everything as normal, i.e. get the response and then get the response Stream and start reading. The data at the beginning of the Stream will be the correct data.
Re: Using Seek() method for GetResponseStream()
Quote:
Originally Posted by
jmcilhinney
No. You can't assign anything to an object. An object is what gets assigned. GetResponseStream returns a Stream object and you can then assign that object to a variable.
Means, in the below line, abc is not declared as an object of Stream class and assigned with the stream from GetResponseStream() method ?
Code:
Dim abc as Stream = web.GetResponseStream()
Code:
Dim abc as new Stream
The above line is declaring and creating the object. Isn't it ?
Quote:
Originally Posted by
jmcilhinney
When the user resumes, you call the AddRange method of the HttpWebRequest to specify where to start reading. If you read 10 blocks before the user paused then you would pass (10 * 4096) as the argument to AddRange to specify that you wanted to read the range of data from that point to the end of the file. You then do everything as normal, i.e. get the response and then get the response Stream and start reading. The data at the beginning of the Stream will be the correct data.
Thanks for that idea. :thumb:
The pausing and resuming is working. I just need to play with other conditions that I used in downloading part to fine tune it. :)
:wave:
Re: Using Seek() method for GetResponseStream()
Quote:
Originally Posted by
akhileshbc
Means, in the below line, abc is not declared as an object of Stream class and assigned with the stream from GetResponseStream() method ?
Code:
Dim abc as Stream = web.GetResponseStream()
Code:
Dim abc as new Stream
The above line is declaring and creating the object. Isn't it ?
You can't declare objects. 'abc' is not an object. It's a variable. They are two different things. You declare variables and you create objects and then you assign the object to the variable. In this case, GetResponseStream returns a Stream object and you then assign that to the variable that you declared. Think about it. In this case:
Code:
Dim o1 As Object = New Object
Dim o2 As Object = o1
Dim o3 As Object = o2
how many objects are there? Is there 1 or 3? Obviously there's only one object because that's all is created. There are three variables and the same object is assigned to all three. Hopefully that makes it clear what the difference is.
Re: Using Seek() method for GetResponseStream()
Thanks for clarifying it. :thumb:
:wave: