Am I able to reuse a HttpWebRequest?
It seems like the 3rd request to a site causes a operation to time out. It seems like each request creates a new connection, so I want to know if I can reuse a HttpWebRequest by changing the url and getting the request again. The code in question is below. This code checks if a range of urls exists.
static void storeList(TextWriter sw, string urlTemplate, int start, int end)
{
for (int i = start; i < end; i++)
{
var url = string.Format(urlTemplate, i);
var req = (HttpWebRequest)HttpWebRequest.Create(url);
{
req.Method = "HEAD";
tryHttpWebRequest
{
var resp = req.GetResponse();
sw.WriteLine(i);
}
catch (Exception e)
{
}
}
}
sw.Flush();
}
You should be ok if you just call Close on your response. You are only allowed so many “open” connections, so the reason it is failing is because it can’t open a new connection.
Once you are done with the response, you need to close it… no need to reuse anything.
From the MSDN article: