I have the following code, and after ~60 times calling it (20 concurrent connections) it starts timing out. if i lower the timeout from 10 minutes to 1 minute, they start timing out at ~34 downloads. what gives? i know that you can get this if you don't properly close your response, but i'm definitely closing it:
//===============================================================================
/// <summary>
/// Executes the request and returns the response as a byte array. Useful if the
/// response should return a file.
/// </summary>
private static byte[] GetResponse(HttpWebRequest webRequest)
{
//---- declare vars
HttpWebResponse response = null;
List<byte> buffer = new List<byte>();
int readByte;
//---- try to get the response, always wrap it.
try
{ response = webRequest.GetResponse() as HttpWebResponse; }
//---- catch all
catch (Exception e)
{
if (response != null) { response.Close(); }
throw new ConnectionFailedException("Failed to get a response", e);
}
try
{
//---- if the response is ok
if (response.StatusCode == HttpStatusCode.OK)
{
//---- get the response stream
using (Stream stream = response.GetResponseStream())
{
//---- read each byte, one by one into the byte buffer
while ((readByte = stream.ReadByte()) > -1)
{
buffer.Add((byte)readByte);
}
//---- close the stream
stream.Close();
response.Close();
}
//---- return the buffer as a byte array
return buffer.ToArray();
}
//---- if the request wasn't auth'd
else if (response.StatusCode == HttpStatusCode.Forbidden || response.StatusCode == HttpStatusCode.Unauthorized)
{
if (response != null) { response.Close(); }
throw new AuthenticationFailedException(response.StatusDescription);
}
//---- any other errors
else
{
if (response != null) { response.Close(); }
throw new ConnectionFailedException(response.StatusDescription);
}
}
finally { if (response != null) { response.Close(); } }
}
//===============================================================================
thoughts?
also, i'm creating it with both the TimeOut and ReadWriteTimeout set to 10 minutes:
//---- create the web request HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
//---- set a 10 minute timeout webRequest.Timeout = 600000; webRequest.ReadWriteTimeout = 600000;