Hi
Is it ok to embed a "try/catch" within a "using" statement for a web request? Is my code correct? That is my requirements are:
Want to use the "using" statement to make sure resources are released in any case for HttpWebResponse
- But still want to do some custom stuff if there is an exception re HttpWebResponse and "response = (HttpWebResponse)request.GetResponse();" in particular.
My source code:
var result = new HttpHeaderInfo();
HttpWebRequest request = null;
HttpWebResponse response = null;
using (response)
{
try
{
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "HEAD";
request.KeepAlive = false;
request.Timeout = Properties.Settings.Default.WebTimeoutDefault;
response = (HttpWebResponse)request.GetResponse();
result.LastModified = response.LastModified;
result.ContentType = response.ContentType;
result.StatusCode = response.StatusCode;
result.ContentLength = response.ContentLength;
}
catch (Exception ex)
{
if (ex is InvalidOperationException ||
ex is ProtocolViolationException ||
ex is WebException)
{
result.HttpError = ex;
result.LastModified = System.DateTime.MinValue;
result.ContentType = null;
}
else { throw; }
}
}
thanks