.net 通过 https 调用获取 EOF 异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5382548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting EOF exception over https call
提问by Amzath
My code making https request. This is my code
我的代码发出 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);
}
This code was running good but all of a sudden throwing following exception
这段代码运行良好,但突然抛出以下异常
System.Net.WebExceptionThe exception message is: The underlying connection was closed: An unexpected error occurred on a send.
Stack Trace: at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) at System.Net.HttpWebRequest.GetRequestStream() at CustomerProcessor.Delivery.Deliver(String content, Int32 productCategory, String identifier, String xsltFile)
The Exception has an inner exception: An Exception occurred: System.IO.IOException The exception message is: Received an unexpected EOF or 0 bytes from the transport stream.
Stack Trace: at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count) at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult) at System.Net.TlsStream.CallProcessAuthentication(Object state) at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.ConnectStream.WriteHeaders(Boolean async)
System.Net.WebException异常消息是:基础连接已关闭:发送时发生意外错误。
堆栈跟踪:在 System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) 在 System.Net.HttpWebRequest.GetRequestStream() 在 CustomerProcessor.Delivery.Deliver(String content, Int32 productCategory, String identifier, String xsltFile)
异常有一个内部异常:发生异常:System.IO.IOException 异常消息是:从传输流接收到意外的 EOF 或 0 字节。
堆栈跟踪:在 System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) 在 System.Net 的 System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)。 Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.StartSendBlob(Byte[] 传入, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult) at System.Net.TlsStream。CallProcessAuthentication(Object state) at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback) callback, Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
在 System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.ConnectStream.WriteHeaders(Boolean异步)
Is there any chance to see what could be the cause of this issue?
有没有机会看到这个问题的原因是什么?
回答by Petr Dokoupil
Adding this call before request helped me:
在请求之前添加此调用对我有帮助:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
So if you are using https try to change the default SecurityProtocol.
因此,如果您使用 https,请尝试更改默认的 SecurityProtocol。
回答by elRobbo
Another potential cause of this - if you are running on Windows Server 2003 it supports only SSL 2.0, SSL 3.0, TLS 1.0. All of these protocols are now recommended to be disabled in light of recent exploits that have been found for these protocols, so you may find that a tightly secured server just can't be connected to from windows server 2003 whereas it will be fine from your development machine. There's nothing you can really do about this situation other than ask the destination server administrator's team to allow TLS1.0, or upgrade your production server.
另一个潜在原因 - 如果您在 Windows Server 2003 上运行,它仅支持 SSL 2.0、SSL 3.0、TLS 1.0。鉴于最近发现这些协议的漏洞,现在建议禁用所有这些协议,因此您可能会发现无法从 Windows Server 2003 连接到严格安全的服务器,而从您的开发机。除了要求目标服务器管理员的团队允许 TLS1.0 或升级您的生产服务器之外,对于这种情况,您实际上无能为力。
Source of SSL support on Win2k3Server: http://blogs.msdn.com/b/kaushal/archive/2011/10/02/support-for-ssl-tls-protocols-on-windows.aspx
Win2k3Server 上的 SSL 支持来源:http: //blogs.msdn.com/b/kaushal/archive/2011/10/02/support-for-ssl-tls-protocols-on-windows.aspx
回答by Cosmin
This worked for me:
这对我有用:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
I have put that line before creating the web request.
在创建 Web 请求之前,我已经放置了该行。
回答by Lei Shi
My experienced is just as elRobbo described. I have to upgrade the Win2k3 server to Win2k8.
我的经验和elRobbo描述的一样。我必须将 Win2k3 服务器升级到 Win2k8。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
is only supported in .net 4.5, which is not supported in Win2k3...
仅在 .net 4.5 中支持,在 Win2k3 中不支持...
回答by Jon Hanna
I have no idea why (especially since it goes against docs I've seen on the matter) but I've found that leaving out the assignment of RequestLength. Rather, just write to the request stream without setting the header.
我不知道为什么(特别是因为它违背了我在这件事上看到的文档)但我发现省略了RequestLength. 相反,只需写入请求流而不设置标头。
This also has the advantage that:
这还有一个好处:
using(Stream stm = req.GetRequestStream())
using(StreamWriter sw = new StreamWriter(stm))
{
sw.Write(postData);
}
is to my mind simpler, along with having greater advantages in cases where the data is larger and written piece by piece, or comes from something like an XMLWriterthat can be set up to write straight to the stream in question.
在我看来更简单,并且在数据较大并且逐个写入的情况下具有更大的优势,或者来自XMLWriter可以设置为直接写入相关流的东西。

