HttpWebRequest/Response Timeouts and Connections

S

Steve Binney

I am having a problem with timeouts and a growing number of connections. My
app periodically (every second) makes a Request and reads a response.

The code for HttpWebRequest is as follows:

sendReq = (HttpWebRequest)WebRequest.Create(myUri);
sendReq.Method = "PUT";

Stream tempStream = sendReq.GetRequestStream();

tempStream.Write(PostData,0,PostData.Length);

tempStream.Close();

respReq = (HttpWebRequest)WebRequest.Create(myUri);

HttpWebResponse response = (HttpWebResponse)respReq.GetResponse();

Stream receiveStream = response.GetResponseStream();

receiveStream.Close();

readStream.Close();

response.Close();

When I make repeasted requests, it works OK for a while but eventually I get
timeouts on GetResponse. In TCPView, I can see a two new connections every
time I make a new request. The number of connections continually grows.
If I force garbage collection, the old connects are cleaned up and the app
works better. With occaisional timeouts

How do I keep reusing the connections without forcing GC ? I am closing
the streams and response as the docs indicate.

Steve
 
J

Joerg Jooss

Steve Binney said:
I am having a problem with timeouts and a growing number of
connections. My app periodically (every second) makes a Request and
reads a response.

The code for HttpWebRequest is as follows:

sendReq = (HttpWebRequest)WebRequest.Create(myUri);
sendReq.Method = "PUT";

Stream tempStream = sendReq.GetRequestStream();

tempStream.Write(PostData,0,PostData.Length);

tempStream.Close();

respReq = (HttpWebRequest)WebRequest.Create(myUri);

HttpWebResponse response = (HttpWebResponse)respReq.GetResponse();

Stream receiveStream = response.GetResponseStream();

receiveStream.Close();

readStream.Close();

response.Close();

When I make repeasted requests, it works OK for a while but
eventually I get timeouts on GetResponse. In TCPView, I can see a
two new connections every time I make a new request. The number of
connections continually grows. If I force garbage collection, the
old connects are cleaned up and the app works better. With
occaisional timeouts

How do I keep reusing the connections without forcing GC ? I am
closing the streams and response as the docs indicate.

What you're doing looks weird to me.

1. You send a PUT to the server.
2. You ignore the response to this request. The reponse and its
associated stream become garbage immediately, but are not closed until
GC'ed, thus wasting resources.
3. You send a GET to the URI you've PUT to in step 1.
4. You process the response to the request from step 2.

Of course you can see two requests each time you execute that piece of
code -- you are sending two...

Dropping step 3 and processing the response to step 1 should help.

Cheers,
 
J

junk

[This followup was posted to microsoft.public.dotnet.framework and a
copy was sent to the cited author.]

What you're doing looks weird to me.

1. You send a PUT to the server.
2. You ignore the response to this request. The reponse and its
associated stream become garbage immediately, but are not closed until
GC'ed, thus wasting resources.
3. You send a GET to the URI you've PUT to in step 1.
4. You process the response to the request from step 2.

Of course you can see two requests each time you execute that piece of
code -- you are sending two...
I believe that what you are doing is incorrect and will show the bad
behavior you are seeing. The web server has thread affinity to the
connection it is serving (http 1.1). It will eventually swamp and get
timeouts. I don't know what you intend on doing (some sort of polling);
what I would do is use a form's submit. When the form postback occurs,
you can perform the proper checks and respond accordingly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top