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.