System.ObjectDisposedException: 'Cannot access a disposed object.'
Solution 1
The first using
block is disposing your response. Move your code after this block into the using
statement.
Solution 2
On this line:
StreamReader loResponseStream = new StreamReader(response.GetResponseStream(), enc);
You are trying to access response
, but this object has already been disposed of by the using
statement before.
Edit:
This code should work, disposing of all objects and returning the value:
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode != HttpStatusCode.OK)
{
throw new ApplicationException("error code " + response.StatusCode.ToString());
}
Encoding enc = System.Text.Encoding.GetEncoding(1252);
using(StreamReader loResponseStream = new StreamReader(response.GetResponseStream(), enc))
{
return loResponseStream.ReadToEnd();
}
}
Returning from a using statement is allowed, more reading: using Statement (C# Reference).
Quote from the site:
The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler.
Solution 3
Look at this part:
using (response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode != HttpStatusCode.OK)
{
throw new ApplicationException("error code " + response.StatusCode.ToString());
}
}
//process the response stream ..(json , html , etc.. )
Encoding enc = System.Text.Encoding.GetEncoding(1252);
StreamReader loResponseStream = new
StreamReader(response.GetResponseStream(), enc);
You are accesing response
when constructing your StreamReader
, but this is outside of your using
statement. The using
statement will dispose of response, hence the error.
Comments
-
Bav over 1 year
that is the code i connect to untrusted server but i always get this error i put the code in the using statement but it is not working return empty string also tried and see the link of this issue before but it is not working
private String requestAndResponse(String url) { string responseValue = string.Empty; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = httpMethod.ToString(); HttpWebResponse response = null; // for un trusted servers System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; try { using (response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode != HttpStatusCode.OK) { throw new ApplicationException("error code " + response.StatusCode.ToString()); } } //process the response stream ..(json , html , etc.. ) Encoding enc = System.Text.Encoding.GetEncoding(1252); StreamReader loResponseStream = new StreamReader(response.GetResponseStream(), enc); responseValue = loResponseStream.ReadToEnd(); loResponseStream.Close(); response.Close(); } catch (Exception ex) { throw ex; } return responseValue; }
-
Elmer Dantas over 6 years
response
only exists inside theusing
scope...you're trying to acess it outside. Put your code inside theusing
scope. -
Abbas over 6 years@ElmerDantas actually no,
response
is declared before the statement. -
Jon Skeet over 6 yearsYou're calling
GetResponseStream
outside the using statement. That's definitely broken, and you shouldn't expect that to work. I suggest you edit this question to show the code with it inside theusing
statement, with a clearer description of exactly what's going on, and what steps you've taken to diagnose it. (Do you know that the server is returning data?) It's not clear what this has to do with the visual-studio or .net-framework-version tags, either. -
Elmer Dantas over 6 years@Abbas indeed but he is calling
GetResponseStream
that will only exists inside theusing
-
Abbas over 6 years@ElmerDantas The object (already) exists, it's only disposed of already. Those are two different things.
-