I have the following working code to get a stream from a url:
private Stream GetDownloadStream(string url)
{
Stream stream = null;
AutoResetEvent downloadCompleted = new AutoResetEvent(false);
httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.AllowReadStreamBuffering = false;
httpRequest.BeginGetResponse(
result =>
{
try
{
httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(result);
stream = httpResponse.GetResponseStream();
}
catch (WebException)
{
downloadCompleted.Set();
Abort();
}
finally
{
downloadCompleted.Set();
}
},
null);
bool completed = downloadCompleted.WaitOne(15 * 1000);
if (completed) {
return stream;
}
return null;
}
It doesn’t matter the streams I choose to play. It always returns a stream for the first 6 requests and it returns null on the seven request.
I already tried to increase the timeout to 30 seconds but on the seventh request it won’t enter on the httpRequest.BeginGetResponse callback.
Any ideas why?
You’re hitting the limit on the number of concurrent web requests (which is 6).
Try closing the stream when you’ve finished with it or staggering your requests so that you’re not trying to make too many at once.