Hi,
I am writing a C# app. I have a finite (~50) set of URLs that I am continuously polling in a circular manner. I would like to do this in a way that can send out each of the requests and get responses as quickly as possible, then do it again.
For my last attempt, I tried implementing a circular queue of precreated HttpWebRequest objects, then creating a HttpWebResponse , StreamReader, and Stream and disposing of them after usage.
My problem is, when I reach the beginning of the queue the second time, I get an exception stating that the "Stream is not readable". It looks like I am not currently able to reuse a HttpWebRequest, at least with out doing something differently. Is it possible to reissue a HttpWebRequest that has already been issued?
I am aware I can do things like async stuff and multithreading to speed things up, but I would really like to understand why this code doesn't work. I also know that I could create a new HttpWebRequest each time, but I did that previously and ran out of ephemeral ports in Windows - so I would like to use the same set of ports if possible.
Also, do you know of any way to determine if a website (yahoo, for example) confirms to the HTTP recommended maximum number of 2 simultaneous connections?
The code looks roughly as follows:
forever{
HttpWebRequest objRequest = urlQueue.GetNex tUrl();
//(objRequest.Kee pAlive = true;)
//(objRequest.Pro xy = WebProxy.GetDef aultProxy();)
//(objRequest.Tim eout = 2000;)
try
{
WebResponse objResponse;
objResponse = objRequest.GetR esponse();
Stream stream = objResponse.Get ResponseStream( );
StreamReader sr = new StreamReader(st ream);
string result = sr.ReadToEnd();
sr.Close();
objResponse.Clo se();
stream.Close();
} catch{}
}
Thanks in advance!!
I am writing a C# app. I have a finite (~50) set of URLs that I am continuously polling in a circular manner. I would like to do this in a way that can send out each of the requests and get responses as quickly as possible, then do it again.
For my last attempt, I tried implementing a circular queue of precreated HttpWebRequest objects, then creating a HttpWebResponse , StreamReader, and Stream and disposing of them after usage.
My problem is, when I reach the beginning of the queue the second time, I get an exception stating that the "Stream is not readable". It looks like I am not currently able to reuse a HttpWebRequest, at least with out doing something differently. Is it possible to reissue a HttpWebRequest that has already been issued?
I am aware I can do things like async stuff and multithreading to speed things up, but I would really like to understand why this code doesn't work. I also know that I could create a new HttpWebRequest each time, but I did that previously and ran out of ephemeral ports in Windows - so I would like to use the same set of ports if possible.
Also, do you know of any way to determine if a website (yahoo, for example) confirms to the HTTP recommended maximum number of 2 simultaneous connections?
The code looks roughly as follows:
forever{
HttpWebRequest objRequest = urlQueue.GetNex tUrl();
//(objRequest.Kee pAlive = true;)
//(objRequest.Pro xy = WebProxy.GetDef aultProxy();)
//(objRequest.Tim eout = 2000;)
try
{
WebResponse objResponse;
objResponse = objRequest.GetR esponse();
Stream stream = objResponse.Get ResponseStream( );
StreamReader sr = new StreamReader(st ream);
string result = sr.ReadToEnd();
sr.Close();
objResponse.Clo se();
stream.Close();
} catch{}
}
Thanks in advance!!
Comment