IOException reading from HttpWebResponse response stream over SSL
- by Lawrence Johnston
I get the following exception when attempting to read the response from my 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()
...
My code functions without issue when using http.
I am talking to a third-party device; I do not have access to the server code.
My code is as follows:
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();
}
}
}
}
Does anybody have any thoughts about what might be causing it?