Reputation: 1919
I am working on a program that automatically queries a website every 5 seconds. It has been working fine for the last few days, but today when I simply restarted it, it keeps throwing System.ObjectDisposedException
on the line marked underneath. I should mention that accessing this URL via a browser on the same machine works fine.
Code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.bitstamp.net/api/ticker/");
request.Method = "GET";
try
{
// ObjectDisposedException thrown here
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
string resultString = reader.ReadToEnd();
return resultString;
}
}
}
catch (WebException ex)
{
// Handle it
}
Stack Trace:
System.ObjectDisposedException occurred
_HResult=-2146232798
_message=Cannot access a disposed object.
HResult=-2146232798
IsTransient=false
Message=Cannot access a disposed object.
Object name: 'SslStream'.
Source=System
ObjectName=SslStream
StackTrace:
at System.Net.Security.SslState.ValidateCreateContext(Boolean isServer, String targetHost, SslProtocols enabledSslProtocols, X509Certificate serverCertificate, X509CertificateCollection clientCertificates, Boolean remoteCertRequired, Boolean checkCertRevocationStatus, Boolean checkCertName)
InnerException:
Is there something I am doing wrong? I do not even access the response stream before the using
, how can it be disposed?
EDIT: Added url and stack trace
Upvotes: 3
Views: 3335
Reputation: 7065
Simplifying things a bit and just doing a:
WebRequest.Create("https://www.bitstamp.net/api/ticker/").GetResponse();
I'm getting WebException "The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF", which apparently is indicative of a problem at the server end, and one (perhaps the only) way round it is to add the following to your config file:
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
Worked for me anyway.
Upvotes: 3