通过SSL读取HttpWebResponse响应流的IOException

时间:2010-06-11 21:08:09

标签: c# ssl httpwebrequest

尝试从HttpWebRequest读取响应时出现以下异常:

System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream.
   at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.IO.StreamReader.ReadBuffer()
   at System.IO.StreamReader.ReadToEnd()
   ...

使用http时,我的代码功能没有问题。

我正在和第三方设备通话;我无权访问服务器代码。

我的代码如下:

private string MakeRequest() {
   // Disable SSL certificate verification per
   // http://www.thejoyofcode.com/WCF_Could_not_establish_trust_relationship_for_the_SSL_TLS_secure_channel_with_authority.aspx
   ServicePointManager.ServerCertificateValidationCallback =
        new RemoteCertificateValidationCallback(delegate { return true; });

    Uri uri = new Uri("https://mydevice/mypath");
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    request.Method = WebRequestMethods.Http.Get;

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
        using (Stream responseStream = response.GetResponseStream()) {
            using (StreamReader sr = new StreamReader(responseStream.)) {
                return sr.ReadToEnd();
            }
        }
    }
}

有没有人对可能导致它的原因有什么想法?

0 个答案:

没有答案