I have a helper class with a method that is set up to take in information to make a HttpWebRequest
. The method as it currently is, returns a HttpWebResponse
.
HttpWebResponse httpResponse;
//make request here
httpResponse = (HttpWebResponse)httpReq.GetResponse();
return httpResponse;
Now this is causing issues because the connections are never being closed. Using the using statement or a close in the callee will destroy the object before it's returned. The right way to do this would be something like.
using (httpResp = (HttpWebResponse)httpReq.GetResponse())
{
return GetDataFromResponse(httpResp);
}
But unfortunately there are many calls into the helper class that are expecting the response object back and then serialize the data in the caller's own way.
I tried doing this on the calls into the helper.
using(HttpWebResponse httpResponseRecieved = WebHelper.MakeRequest(parameters))
but this doesn't seem to close the response. Is httpResponseRecieved
a different object than the one returned? Is there a better solution or will I have to rewrite the method and all of its callers?
As @JohnSaunders pointed out, the last using
that is wrapped around the caller is closing the response.
So doing using(HttpWebResponse httpResponseRecieved = WebHelper.MakeRequest(parameters))
works after all.