473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

WebRequest times out...but only 3rd time and on...

Hi,

I put this code in a windows forms app for testing. The IP values are both
correct. It works the first time, the second time but at the third time it
fails with timeout... I have it running here AND in a service with a 30
minute interval.

1. Am I doing this correclty? Is there a better way to go to a URI and pass
a couple of variables?

2. If I am doing this correctly any idea why it would be timing out on the
third time onwards? The ASP page just takes the values and writes them to a
DB. If I run it in a browser I can run it over and over with no timeouts...I
think it must be something that I am doing but I can't thing what.

Can someone help?

Tim

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
static System.Timers.Timer timer = new System.Timers.Timer(5000);
public Form1()
{
InitializeComponent();
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timer.Stop();
Console.WriteLine("Timer Elapsed");
System.Net.WebRequest req =
System.Net.WebRequest.Create(http://24.36.xxx.xxx/server/update_i...7.70.xxx.xxx);

req.GetResponse();
Console.WriteLine("Done");
Console.WriteLine();
timer.Start();
}
}
}
Feb 23 '06 #1
9 2041
Tim Cowan wrote:
I put this code in a windows forms app for testing. The IP values are both
correct. It works the first time, the second time but at the third time it
fails with timeout... I have it running here AND in a service with a 30
minute interval.

1. Am I doing this correclty? Is there a better way to go to a URI and pass
a couple of variables?


I don't have time to look at the rest of your code in detail at the
minute, but I believe the problem is that you're not disposing of the
WebResponse. If you do, you should notice everything being fine. .NET
is happy to have two connections up at a time, but your existing
responses are "hogging" the connections it's made.

Jon

Feb 23 '06 #2
Thanks Jon, that was it. As I wasn't putting it into a reponse object I
couldn't see it, but now it works great.
System.Net.WebResponse resp = req.GetResponse();
resp.Close();

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Tim Cowan wrote:
I put this code in a windows forms app for testing. The IP values are
both
correct. It works the first time, the second time but at the third time
it
fails with timeout... I have it running here AND in a service with a 30
minute interval.

1. Am I doing this correclty? Is there a better way to go to a URI and
pass
a couple of variables?


I don't have time to look at the rest of your code in detail at the
minute, but I believe the problem is that you're not disposing of the
WebResponse. If you do, you should notice everything being fine. .NET
is happy to have two connections up at a time, but your existing
responses are "hogging" the connections it's made.

Jon

Feb 23 '06 #3
Tim Cowan <ti******@sympatico.ca> wrote:
Thanks Jon, that was it. As I wasn't putting it into a reponse object I
couldn't see it, but now it works great.
System.Net.WebResponse resp = req.GetResponse();
resp.Close();


A generally better approach would be:

using (WebResponse resp = req.GetResponse())
{
// Anything else here
}

That way the response will be disposed even if an exception is thrown.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 23 '06 #4
jon

if you have a minute, can you comment on when the code should be as
you have it above and when it should be:

using (WebResponse resp = req.GetResponse())
{
// Anything else here
GC.KeepAlive(resp); <---------
}

it was my understanding that the 'using' statement expanded to
something like:

try{
// the code
}
finally
{
theObjectInTheUsing.Dispose();
}

thus the GC.KeepAlive() seems unnecessary since the object shouldn't be
collected until after the finally block. is it for the scenario that
the object doesn't implement IDisposable's Dispose()?

thanks in advance,
matt

Feb 23 '06 #5
Thanks Jon. I really appreciate it when someone helps me out by suggesting
a better way to code. I am always trying to write more 'elegant' and solid
code.

Thanks

Tim
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Tim Cowan <ti******@sympatico.ca> wrote:
Thanks Jon, that was it. As I wasn't putting it into a reponse object I
couldn't see it, but now it works great.
System.Net.WebResponse resp = req.GetResponse();
resp.Close();


A generally better approach would be:

using (WebResponse resp = req.GetResponse())
{
// Anything else here
}

That way the response will be disposed even if an exception is thrown.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Feb 23 '06 #6
Thus wrote Tim,
Hi,

I put this code in a windows forms app for testing. The IP values are
both correct. It works the first time, the second time but at the
third time it fails with timeout... I have it running here AND in a
service with a 30 minute interval.

1. Am I doing this correclty? Is there a better way to go to a URI and
pass a couple of variables?
Has been answered :-)
2. If I am doing this correctly any idea why it would be timing out on
the third time onwards? The ASP page just takes the values and writes
them to a DB. If I run it in a browser I can run it over and over with
no timeouts...I think it must be something that I am doing but I can't
thing what.


Because two is the number of persistent ("keep-alive") HTTP connections a
client should establish to one server according to the HTTP 1.1 spec (RFC
2616).

Your not-disposed-of responses actually block all underlying TCP connections
to the web server, until your code either times out or at least one response
has been finalized and disposed of.

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
Feb 23 '06 #7
<ma**********@yahoo.com> wrote:
if you have a minute, can you comment on when the code should be as
you have it above and when it should be:

using (WebResponse resp = req.GetResponse())
{
// Anything else here
GC.KeepAlive(resp); <---------
}


You never need that KeepAlive, you're right. And if the type returned
by resp doesn't implement IDisposable, the statement won't compile in
the first place.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 23 '06 #8
that's what i was thinking, but a bit back i read this article on
theserverside.net:

http://www.theserverside.net/article...bageCollection

which confused me as they say that it was the usage of GC.KeepAlive()
in a using block which solved their bug. however, could have been more
of a outlier-type scenario? they talk about having a 'proxy' as well
which is probably the root of the problem (not that the proxy *is* the
problem, per sa, but rather just part of the complicated equation) in
that the proxy and the 'real' object are disposed of as separate
entities as opposed to an atomic instance.

great catch on IDisposable. that was just silly on my part. oh well,
i'm past it now.

thanks again!

Feb 23 '06 #9
<ma**********@yahoo.com> wrote:
that's what i was thinking, but a bit back i read this article on
theserverside.net:

http://www.theserverside.net/article...bageCollection

which confused me as they say that it was the usage of GC.KeepAlive()
in a using block which solved their bug. however, could have been more
of a outlier-type scenario? they talk about having a 'proxy' as well
which is probably the root of the problem (not that the proxy *is* the
problem, per sa, but rather just part of the complicated equation) in
that the proxy and the 'real' object are disposed of as separate
entities as opposed to an atomic instance.
I strongly suspect that GC.KeepAlive wasn't actually what solved the
problem for them. It wouldn't be the first time I've seen someone apply
two changes at once and declare them both necessary, without having
tested each separately.

There are oddities with garbage collection to be sure, but none that
would do that, IMO.

Interestingly, they *do* have a bug in their Dispose implementation.
They set inst to 0 and don't use it any further in the method, but
that's *before* they call GC.SuppressFinalize. I suspect they may have
seen finalization taking place between the call to Interlocked.Exchange
and the call to SuppressFinalize, which would have caused them some
grief...

Anyway, I'd want to see clear evidence of the finalizer being called
within the using block before believing it.
great catch on IDisposable. that was just silly on my part. oh well,
i'm past it now.


:)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: omyek | last post by:
I'm essentially trying to do what a lot of users seem to want when using the above classes, and that's POST to a webpage. Well, I'm golden when it comes to POSTing, I've been able to post to...
0
by: omyek | last post by:
I'm essentially trying to do what a lot of users seem to want when using the above classes, and that's POST to a webpage. Well, I'm golden when it comes to POSTing, I've been able to post to...
2
by: kkb | last post by:
Hello! First, I'm sorry because of my english... I'll try to be understandable! I've got a strange problem using .NET 2003 C# and I haven't figured it out for a long time. I'm implementing an...
3
by: rashid.anwer | last post by:
We have pro hosting on networksolutions for an ecommerce site. We are trying to connect to authorize.net via webrequest. It seems that NetworkSolutions has medium trust level and overriding is...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.