Mysterious delay on first use of HttpWebRequest.GetRequestStream

  • Thread starter Carl Daniel [VC++ MVP]
  • Start date
C

Carl Daniel [VC++ MVP]

I've got some code that looks like this (yes, I should be using 'using', but
that's not relevant to the problem at hand):

//
// Create an HTTP request to do the POST
//
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(Context.Current.URL);
req.Method = "POST";

//
// Write the serialized XML to the request
//
Stream rs = req.GetRequestStream();
XmlTextWriter tw = new XmlTextWriter(rs,Encoding.GetEncoding("UTF-8"));
doc.WriteTo(tw);

// Force the XmlTextWriter to finish
tw.Flush();

// we must close the stream to actually initiate sending the request
rs.Close();

//
// Get the response from the server
//
req.Timeout = 600000;
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

//
// Parse the response into a new XmlDocument & return it
//
m_lastResponse = new XmlDocument();
m_lastResponse.Load(resp.GetResponseStream());

I've used this code in many programs for the past 3 years without any
problems or odd behavior. Recently though, I ported it to .NET 2.0 and used
it in a new program - a simple WinForms app.

On the very first entry into this code, there's a delay of ~30-60 seconds,
apparently within the call to req.GetRequestStream.

The docs for GetRequestStream say "You must set the value of the
ContentLength property before retrieving the stream". That statement is not
true - it really should say "If you need to set the ContentLength property,
you need to do so before calling GetRequestStream". In any case, I modified
the code to set ContentLength and it made no difference. I set a few other
properties too:

HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(Context.Current.URL);
req.Method = "POST";
req.ContentType = "text/xml";
req.ContentLength = bytes.Length;
req.AllowAutoRedirect = false;
req.AllowWriteStreamBuffering = true;
req.AuthenticationLevel =
System.Net.Security.AuthenticationLevel.None;
req.ImpersonationLevel =
System.Security.Principal.TokenImpersonationLevel.None;
req.UserAgent = "My agent string";

//
// Write the serialized XML to the request
//
Stream rs = req.GetRequestStream();
rs.Write(bytes, 0, bytes.Length);

These changes made no difference - there's still a long delay on the first
call to GetRequestStream.

Environment is .NET 2.0 on XP SP2, the URL refers to a machine that's
reachable over an L2TP VPN connection through a DSL modem. After that first
really slow connection, all other access is speedy like I'd expect. Other
HTTP access over the VPN connection to the same machine doesn't appear to
suffer from the same initial connection delay, so I don't think it's a VPN
or TCP/IP problem, but rather something .NET 2.0 specific.


This same code, same machine, same VPN, same target server does not produce
the mysterious delay when built for and run on .NET 1.1 it definitely feels
like there's something new in 2.0 that's tripping me up.

Any ideas?

-cd
 
C

Carl Daniel [VC++ MVP]

Carl said:
Environment is .NET 2.0 on XP SP2, the URL refers to a machine that's
reachable over an L2TP VPN connection through a DSL modem. After
that first really slow connection, all other access is speedy like
I'd expect. Other HTTP access over the VPN connection to the same
machine doesn't appear to suffer from the same initial connection
delay, so I don't think it's a VPN or TCP/IP problem, but rather
something .NET 2.0 specific.

Indeed!

Despite the fact that no proxy is configured on the machine, and IE/WinINet
are configured to never use a proxy, with automatic detection disabled,
HttpWebRequest was apparently trying to detect a proxy.

Set req.Proxy = null

to get the 1.1 behavior.

-cd
 
J

Jay B. Harlow [MVP - Outlook]

Carl,
Just out of curiosity, how is your deafultProxy setting in your web.config /
app.config set?

http://msdn2.microsoft.com/en-us/library/kd3cf2ex(VS.80).aspx

When I started researching a project that uses HttpWebRequest I noticed the
above setting...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


"Carl Daniel [VC++ MVP]" <[email protected]>
wrote in message | Carl Daniel [VC++ MVP] wrote:
| > Environment is .NET 2.0 on XP SP2, the URL refers to a machine that's
| > reachable over an L2TP VPN connection through a DSL modem. After
| > that first really slow connection, all other access is speedy like
| > I'd expect. Other HTTP access over the VPN connection to the same
| > machine doesn't appear to suffer from the same initial connection
| > delay, so I don't think it's a VPN or TCP/IP problem, but rather
| > something .NET 2.0 specific.
|
| Indeed!
|
| Despite the fact that no proxy is configured on the machine, and
IE/WinINet
| are configured to never use a proxy, with automatic detection disabled,
| HttpWebRequest was apparently trying to detect a proxy.
|
| Set req.Proxy = null
|
| to get the 1.1 behavior.
|
| -cd
|
|
 
C

Carl Daniel [VC++ MVP]

Jay said:
Carl,
Just out of curiosity, how is your deafultProxy setting in your
web.config / app.config set?

http://msdn2.microsoft.com/en-us/library/kd3cf2ex(VS.80).aspx

When I started researching a project that uses HttpWebRequest I
noticed the above setting...

It's not set at all in app.cong or machine.config. Hmm. If I'm reading the
MSDN docs right, the default for defaultProxy.enabled is TRUE if the element
isn't specified at all. That would be consistent with my observations.

Indeed, adding

<system.net>
<defaultProxy
enabled="false"
useDefaultCredentials="false" >
<proxy/>
<bypasslist/>
<module/>
</defaultProxy>
</system.net>

to my app.config got things going again. Thanks for the pointer! They sure
could've done a better job at pointing out this change in behavior.

-cd
 
J

Jay B. Harlow [MVP - Outlook]

Carl,
| It's not set at all in app.cong or machine.config. Hmm. If I'm reading
the
| MSDN docs right, the default for defaultProxy.enabled is TRUE if the
element
| isn't specified at all. That would be consistent with my observations.
That's the way I was reading it, that the default was TRUE.

Thank you for confirming the behavior.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


"Carl Daniel [VC++ MVP]" <[email protected]>
wrote in message | Jay B. Harlow [MVP - Outlook] wrote:
| > Carl,
| > Just out of curiosity, how is your deafultProxy setting in your
| > web.config / app.config set?
| >
| > http://msdn2.microsoft.com/en-us/library/kd3cf2ex(VS.80).aspx
| >
| > When I started researching a project that uses HttpWebRequest I
| > noticed the above setting...
|
| It's not set at all in app.cong or machine.config. Hmm. If I'm reading
the
| MSDN docs right, the default for defaultProxy.enabled is TRUE if the
element
| isn't specified at all. That would be consistent with my observations.
|
| Indeed, adding
|
| <system.net>
| <defaultProxy
| enabled="false"
| useDefaultCredentials="false" >
| <proxy/>
| <bypasslist/>
| <module/>
| </defaultProxy>
| </system.net>
|
| to my app.config got things going again. Thanks for the pointer! They
sure
| could've done a better job at pointing out this change in behavior.
|
| -cd
|
|
|
 

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