0

Here is my code:

 public static Stream CreateLink(Uri path, int start, int end)
        {
            HttpWebResponse response;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);
            request.Timeout = 30000;
            request.AddRange(start, end);
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch
            {
                response = null;
            }
            if (response != null)
            {
                var stream = response.GetResponseStream();
                return stream;
            }
            return null;
        }

I am creating multiple connections to download data in parallel from same stream.However, it is returning the stream once and returning null in all subsequent attempts till first returned stream is closed.
Moreover, Stream supports Accept-Ranges as bytes.
So, My question is how can I make multiple connections or Is something wrong in my above code?


Update:
response is set to null due to timeout exception or precisely no response is getting till previous connection(response stream) is closed.

userda
  • 595
  • 5
  • 15

2 Answers2

0

Seems like most times you won't get the length in an HttpWebRequest, and this is why when trying to run your code you'll get exceptions like:

This stream does not support seek operations.

and:

'debug.Position' threw an exception of type 'System.NotSupportedException'

Have a look at this answer for more details :)

Edit: I'm not sure how you're calling your code and from where, but if you're doing it in a normal procedural (serial) way, for example:

for (int i =0; i<10; i++) {
  var some_var = CreateLink(path, i*100,(i+1)*100)
}

Then it'll obviously call the method, wait until it gets the first 100 (or whatever you set) bytes, then call it again.

Community
  • 1
  • 1
Noctis
  • 11,507
  • 3
  • 43
  • 82
  • I am not getting such exception as I am not using seek operation.I just get timeout exception or precisely I am not able to create more than one active connection at a time. – userda Nov 02 '13 at 12:31
  • Also as side note: I have length otherwise I would not have start and end – userda Nov 02 '13 at 12:38
  • That's the error I got when I tried your code, can you give me the link you're using? – Noctis Nov 02 '13 at 23:34
0

.NET Framework 4/4.5 have built-in optimized async HttpClient classes. You can use them to achieve almost all that you want from HTTP. Here is all that you need:

var responseMessage = await (new HttpClient().GetAsync("http://download.linnrecords.com/test/flac/recit24bit.aspx", HttpCompletionOption.ResponseHeadersRead));
if (responseMessage.StatusCode == System.Net.HttpStatusCode.OK)
{
    var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test_http_download.flac");
    using (var fileStream = File.Create(filePath))
    using (var httpStream = await responseMessage.Content.ReadAsStreamAsync())
    {
        httpStream.CopyTo(fileStream);
        fileStream.Flush();
    }
    Process.Start(filePath);
}
AuthorProxy
  • 7,946
  • 3
  • 27
  • 40