Bav Bav - 2 years ago 91
C# Question

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download