通过https调用获取EOF异常

Amz*_*ath 15 .net http

我的代码发出https请求.这是我的代码

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(UploadUrl);
            request.Method = "POST";
            request.KeepAlive = false;
            request.Credentials = new NetworkCredential(userid, testpwd);

            postData = "<root></root>";
            request.ContentType = "application/x-www-form-urlencoded";

            byte[] postDataBytes = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = postDataBytes.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(postDataBytes, 0, postDataBytes.Length);
            requestStream.Close();

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                StreamReader responseReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                var result = responseReader.ReadToEnd();
                responseReader.Close();
                Console.WriteLine(result);
            }
Run Code Online (Sandbox Code Playgroud)

这段代码运行良好但突然抛出异常

System.Net.WebException
Run Code Online (Sandbox Code Playgroud)

异常消息是:基础连接已关闭:发送时发生意外错误.

堆栈跟踪:位于System.Net.HttpWebRequest.GetRequestStream(TransportContext&context)的System.Net.HttpWebRequest.GetRequestStream(),位于CustomerProcessor.Delivery.Deliver(String content,Int32 productCategory,String identifier,String xsltFile)

Exception有一个内部异常:发生异常:System.IO.IOException异常消息是:从传输流中收到意外的EOF或0字节.

堆栈跟踪:System.Net上的System.Net.FixedSizeReader.ReadPacket(Byte []缓冲区,Int32偏移量,Int32计数)处于System.Net的System.Net.Security.SslState.StartReadFrame(Byte []缓冲区,Int32 readBytes,AsyncProtocolRequest asyncRequest).在System.Net.Security.SslState.StartSendBlob(Byte []传入,Int32计数,System.SNet.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken消息,AsyncProtocolRequest asyncRequest)上的Security.SslState.StartReceiveBlob(Byte []缓冲区,AsyncProtocolRequest asyncRequest),位于System.Net.TlsStream.CallProcessAuthentication(对象状态)的System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)的System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst,Byte [] buffer,AsyncProtocolRequest asyncRequest)中的AsyncProtocolRequest asyncRequest) )System.Threading.ExecutionContext.runTryCode(Object userData)at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code,CleanupCode backoutCode,对象userData)System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调,对象状态)System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)在System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult结果) )
在System.Net.ConnectStream.WriteHeaders上的System.Net.TooStream.Write(Byte []缓冲区,Int32偏移量,Int32大小)处于System.Net.PooledStream.Write(Byte []缓冲区,Int32偏移量,Int32大小)处(布尔异步)

有没有机会看到这个问题可能是什么原因?

小智 21

在请求之前添加此调用帮助我:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
Run Code Online (Sandbox Code Playgroud)

因此,如果您使用https,请尝试更改默认的SecurityProtocol.


elR*_*bbo 11

另一个潜在原因 - 如果您在Windows Server 2003上运行,它仅支持SSL 2.0,SSL 3.0,TLS 1.0.现在建议根据最近为这些协议找到的漏洞禁用所有这些协议,因此您可能会发现无法从Windows Server 2003连接到严格保护的服务器,而您可以从开发机器.除了要求目标服务器管理员的团队允许TLS1.0或升级您的生产服务器之外,您无法真正做到这一点.

Win2k3Server上的SSL支持来源:http://blogs.msdn.com/b/kaushal/archive/2011/10/02/support-for-ssl-tls-protocols-on-windows.aspx

  • 你买过那种啤酒吗? (7认同)

Cos*_*min 6

这对我有用:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Run Code Online (Sandbox Code Playgroud)

在创建 Web 请求之前,我已经放置了该行。