HttpWebResponse seems to hang

N

Nuno Magalhaes

Hello,

In a simple thread I have a code like the one below:
public void ProtectionRun()
{
while(true)
{
//Sleep thread for one minute
//Thread.Sleep(60000);
HttpWebRequest
wReq=(HttpWebRequest)WebRequest.Create("http://pipa.inov.pt/cgi-bin/camera");

HttpWebResponse wRes=(HttpWebResponse)wReq.GetResponse();
MessageBox.Show("Completed...");
}
}


The problem is: "It only completes 2 times...". Why doesn't it get
completed all the times I want... and why does it hangs (on the thread
of course) in GetResponse() method?

The strange thing is that when I try to access any of the wRes
properties it doesn't complete nothing at all. More: it doesn't give
any exceptions... nothing... it seems like the function evaporates to
nowhere.

Tried with different websites and all give the same problem... hangs up
at the 3rd time. I'm not seeing the complete message for the third time
in all types of webpages (static .html; .asp; etc...)

Thanks for any help,
Nuno Magalhaes.
 
C

Cor Ligthert [MVP]

Nuno,

Just guessing, AFAIK does standard with http 1.1 a computer handles only 2
http requests in the same time. (AFAIK is this build in by the builders of
the operating systems, this is an agreement with Internet providers)

Cor
 
G

Guest

Hi Nuno. Actually it does throw an exception, a System.Net.WebException
after 100 seconds (the default timeout for Web Responses).

It's being caused by not closing your resources. You need to close your
HttpWebResponse when you are done with it. Here is the modified code that
works.

HttpWebResponse wRes = null;

try
{
while(true)
{
//Sleep thread for one minute
//Thread.Sleep(60000);
HttpWebRequest wReq=(HttpWebRequest)
WebRequest.Create("http://pipa.inov.pt/cgi-bin/camera");

wRes =(HttpWebResponse)wReq.GetResponse();

MessageBox.Show("Completed...");

// Close the wRes object. Limited resources available.
wRes.Close();
wRes = null;
}
}
catch(System.Exception ex)
{
// Handle the exception appropriately
Debug.WriteLine(ex.ToString());
}
finally
{
// Clean up the resource in case it wasn't cleaned up previously.
if ( wRes != null )
wRes.Close();
}

Hope this helps.
- Jason Bartosch
 
N

Nuno Magalhaes

It actually helped. Thanks a lot.

Jason said:
Hi Nuno. Actually it does throw an exception, a System.Net.WebException
after 100 seconds (the default timeout for Web Responses).

It's being caused by not closing your resources. You need to close your
HttpWebResponse when you are done with it. Here is the modified code that
works.

HttpWebResponse wRes = null;

try
{
while(true)
{
//Sleep thread for one minute
//Thread.Sleep(60000);
HttpWebRequest wReq=(HttpWebRequest)
WebRequest.Create("http://pipa.inov.pt/cgi-bin/camera");

wRes =(HttpWebResponse)wReq.GetResponse();

MessageBox.Show("Completed...");

// Close the wRes object. Limited resources available.
wRes.Close();
wRes = null;
}
}
catch(System.Exception ex)
{
// Handle the exception appropriately
Debug.WriteLine(ex.ToString());
}
finally
{
// Clean up the resource in case it wasn't cleaned up previously.
if ( wRes != null )
wRes.Close();
}

Hope this helps.
- Jason Bartosch
 

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