简体   繁体   中英

System.ObjectDisposedException: 'Cannot access a disposed object.'

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;
    }

The first using block is disposing your response. Move your code after this block into the using statement.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM