Reputation: 139
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;
}
Upvotes: 3
Views: 22617
Reputation: 14432
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.
Upvotes: 5
Reputation: 11841
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.
Upvotes: 1
Reputation: 879
The first using
block is disposing your response. Move your code after this block into the using
statement.
Upvotes: 10