Exception with HttpWebRequest.GetResponse

R

Roy Chastain

The following is code from the HttpWebRequest.KeepAlive Property documentation
HttpWebRequest myHttpWebRequest2 =
(HttpWebRequest)WebRequest.Create("http://www.contoso.com");
myHttpWebRequest2.Connection="Close";
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse myHttpWebResponse2 =
(HttpWebResponse)myHttpWebRequest2.GetResponse();
// Release the resources held by response object.
myHttpWebResponse2.Close();

The following is my code
wreq = (HttpWebRequest)(WebRequest.Create("http://roy"));
wreq.Connection = "Close";
wresp = (HttpWebResponse)(wreq.GetResponse());
wresp.Close();

The following is the exception (occurs on the GetResponse)
An unhandled exception of type 'System.ArgumentException' occurred in system.dll
Additional information: Keep-Alive and Close may not be set with this property.

1) - Is the example wrong?
2) - What procedure should I use to 'close' my connection to a web server?

Thanks for any insight.
 
S

Steven Cheng[MSFT]

Hi Roy,

Welcome to MSDN newsgroup. As for the problem you eoucntered when set the
Connection property of the HTTPWebRequest, I think the MSDN's document on
the Connection property does be a bit ambigous. In fact, you can find that
the "Exception" section in the document says

The "ArgumentException" will be thrown if we directly set the Connection to
"Close" or "Keep-Alive". And after some further dig into the
HttpWebRequest's code, I think we should use the "KeepAlive" property
instead of the "Connection" property to set the Connection header. For
example:

use webrequest.KeepAlive = false; // to set "Connection:Close"

use webrequest.KeepAlive = true; // to set "Connection:Keep-Alive"

Thanks & Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
J

Joerg Jooss

Roy said:
The following is code from the HttpWebRequest.KeepAlive Property
documentation HttpWebRequest myHttpWebRequest2 =
(HttpWebRequest)WebRequest.Create("http://www.contoso.com");
myHttpWebRequest2.Connection="Close";
// Assign the response object of 'HttpWebRequest' to a
'HttpWebResponse' variable. HttpWebResponse myHttpWebResponse2
= (HttpWebResponse)myHttpWebRequest2.GetResponse();
// Release the resources held by response object.
myHttpWebResponse2.Close();

The following is my code
wreq = (HttpWebRequest)(WebRequest.Create("http://roy"));
wreq.Connection = "Close";
wresp = (HttpWebResponse)(wreq.GetResponse());
wresp.Close();

The following is the exception (occurs on the GetResponse)
An unhandled exception of type 'System.ArgumentException' occurred in
system.dll Additional information: Keep-Alive and Close may not be
set with this property.

1) - Is the example wrong?

Somewhat. You seem to assume that keeping the underlying TCP connection
open is a bad thing, which isn't true. If you close all your managed
resources (see below), there's usually nothing more to worry about.
2) - What procedure should I use to 'close' my connection to a web
server?

Either close The HttpWebResponse object or its response stream. You
already do that, although you should use a using or finally block in
case an exception occurs while processing the response.

Cheers,
 

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