HttpWebRequest reuse problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • matadon
    New Member
    • Feb 2008
    • 2

    HttpWebRequest reuse problems

    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!!
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    You may need to clear the objResponse object. I assume that forever is a function name. Code is never 'roughly' anything, please provide annotation remarks if editing. Thanks.

    Comment

    • matadon
      New Member
      • Feb 2008
      • 2

      #3
      Thanks for the reply!

      forever = while(!done)

      Could you expand on what you mean by clearing the objResponse? Thanks!

      Comment

      • jallred
        New Member
        • Feb 2008
        • 6

        #4
        Originally posted by matadon
        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.

        <...>

        Thanks in advance!!
        See the code at http://www.thescripts. com/forum/showthread.php? p=3075511#post3 075511 for a more typical use of HttpWebRequest.

        John
        http://blogs.msdn.com/usisvde/

        Comment

        Working...