G
George Ter-Saakov
Just learned (hard way) one gotcha about working with WebRequest.
Decided to share with with you guys since it's kind of not documented ( at least I did not see it).
Given following code
try
{
HttpWebRequest httpWebRequest = WebRequest.Create(http://www.site.com);
HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
......
httpWebResponse.Close();
}
catch (WebException e)
{
if (e.Response != null)
e.Response.Close();
.......
}
It's important to catch WebException (and not Exception) since it has a Response object. If you do not close it, you will run into problem. Since only 2 connections (default) to the remote server can be open at the same time.
So 2 "500"' errors and your application will not be able to connect to the server anymore until GC runs or it restart...
PS: So far I was always catching Exception and did not even realized that I must catch WebException and properly close WebException.Response until my "hard landing" today when everything stopped working after 2 legitimate 404 (Not found) returned by the server.
Good luck.
George.
Decided to share with with you guys since it's kind of not documented ( at least I did not see it).
Given following code
try
{
HttpWebRequest httpWebRequest = WebRequest.Create(http://www.site.com);
HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
......
httpWebResponse.Close();
}
catch (WebException e)
{
if (e.Response != null)
e.Response.Close();
.......
}
It's important to catch WebException (and not Exception) since it has a Response object. If you do not close it, you will run into problem. Since only 2 connections (default) to the remote server can be open at the same time.
So 2 "500"' errors and your application will not be able to connect to the server anymore until GC runs or it restart...
PS: So far I was always catching Exception and did not even realized that I must catch WebException and properly close WebException.Response until my "hard landing" today when everything stopped working after 2 legitimate 404 (Not found) returned by the server.
Good luck.
George.