我的代码发出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);
}
此代码运行良好,但突然抛出异常
System.Net.WebException
异常消息是: 基础连接已关闭:发送时发生意外错误。
Stack Trace:at System.Net.HttpWebRequest.GetRequestStream(TransportContext&amp; context) 在System.Net.HttpWebRequest.GetRequestStream()处 CustomerProcessor.Delivery.Deliver(String content,Int32 productCategory,String identifier,String xsltFile)
Exception有一个内部异常:发生异常: System.IO.IOException异常消息为:收到意外的消息 EOF或传输流中的0个字节。
堆栈跟踪:在System.Net.FixedSizeReader.ReadPacket(Byte [] 缓冲区,Int32偏移量,Int32计数)at System.Net.Security.SslState.StartReadFrame(Byte []缓冲区,Int32 readBytes,AsyncProtocolRequest asyncRequest)at System.Net.Security.SslState.StartReceiveBlob(Byte []缓冲区, AsyncProtocolRequest asyncRequest)at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken 消息,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 代码,CleanupCode backoutCode,Object userData)at System.Threading.ExecutionContext.RunInternal(执行上下文 executionContext,ContextCallback回调,对象状态)at System.Threading.ExecutionContext.Run(执行上下文 executionContext,ContextCallback回调,对象状态)at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
在System.Net.TlsStream.Write(Byte []缓冲区,Int32偏移量,Int32大小) 在System.Net.PooledStream.Write(Byte []缓冲区,Int32偏移量,Int32 size)在System.Net.ConnectStream.WriteHeaders(布尔异步)
有没有机会看到这个问题可能是什么原因?
答案 0 :(得分:20)
在请求之前添加此调用帮助了我:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
因此,如果您使用https,请尝试更改默认的SecurityProtocol。
答案 1 :(得分:10)
另一个潜在原因 - 如果您在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
答案 2 :(得分:2)
我的经验就像elRobbo描述的那样。我必须将Win2k3服务器升级到Win2k8。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
仅在.net 4.5中受支持,Win2k3不支持...
答案 3 :(得分:2)
这对我有用:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
我在创建网络请求之前已经放了那条线。
答案 4 :(得分:0)
我不知道为什么(特别是因为它违背了我在此事上看过的文档),但我发现遗漏了RequestLength
的任务。相反,只需在不设置标题的情况下写入请求流。
这也有以下优点:
using(Stream stm = req.GetRequestStream())
using(StreamWriter sw = new StreamWriter(stm))
{
sw.Write(postData);
}
在我看来更简单,同时在数据更大并逐个书写的情况下具有更大的优势,或者来自XMLWriter
之类的东西,可以设置为直接写入流中问题