WebResponse response;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 20000;
response = request.GetResponse();
request = (HttpWebRequest)WebRequest.Create(url2);
response = request.GetResponse();
}
catch(Exception ex)
{
//do something
}
finally
{
}
where should response.Close() be called?
-
after every GetResponse() in try?
-
after last GetResponse() in try – once?
- in finally block?
None of the above. You should be using a
usingblock:A
usingblock will ensure that the Dispose method is called, whether or not there is an exception. Dispose will do the same thing as Close.is equivalent to: