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
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