我正在开发一个每5秒钟自动查询一个网站的程序。它在过去几天一直很好用,但是今天当我重新启动它时,它会不断地将System.ObjectDisposedException
扔到下面标记的线上。我应该提一下,通过同一台机器上的浏览器访问此URL工作正常。
代码:
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
}
堆栈追踪:
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:
我做错了吗?我甚至都没有在using
之前访问响应流,如何处理?
编辑:添加了网址和堆栈跟踪
答案 0 :(得分:3)
稍微简化一下,只做一次:
WebRequest.Create("https://www.bitstamp.net/api/ticker/").GetResponse();
我收到了WebException“服务器提交了一个协议违规.Section = ResponseHeader Detail = CR必须跟着LF”,apparently表示服务器端有问题,一个(也许是只有这样才能将以下内容添加到您的配置文件中:
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
无论如何为我工作。