2011-03-21 91 views
11

我的代碼發出https請求。這是我的代碼通過https獲取EOF異常請致電

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 

異常消息是: 基礎連接已經關閉:發生意外的錯誤發送。

堆棧跟蹤: 在System.Net.HttpWebRequest.GetRequestStream(TransportContext &上下文) 在System.Net.HttpWebRequest.GetRequestStream() 在CustomerProcessor.Delivery.Deliver(字符串的內容,的Int32產品分類,字符串標識符,字符串xsltFile)

異常具有內異常:發生 異常: System.IO.IOException 異常消息爲: 從傳輸流接收到一個意想不到的EOF或0字節。

堆棧跟蹤: 在System.Net.FixedSizeReader.ReadPacket(字節[]緩衝液,的Int32偏移的Int32計數) 在System.Net.Security.SslState.StartReadFrame(字節[]緩衝液,的Int32的ReadBytes,AsyncProtocolRequest asyncRequest ) 在System.Net.Security.SslState.StartReceiveBlob(字節[]緩衝液,AsyncProtocolRequest asyncRequest) 在System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken消息,AsyncProtocolRequest asyncRequest) 在System.Net.Security.SslState.StartSendBlob (Byte [] incoming,Int32 count,AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst,Byte [] buffer,AsyncProtocolRequest asyncRequest) 在System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult) 在System.Net.TlsStream.CallProcessAuthentication(對象狀態) 在System.Threading.ExecutionContext.runTryCode(對象的UserData) 在System.Runtime.CompilerServices.RuntimeHelpers .ExecuteCodeWithGuaranteedCleanup(TryCode代碼,CleanupCode backoutCode,對象的UserData) 在System.Threading.ExecutionContext.RunInternal(的ExecutionContext的ExecutionContext,ContextCallback回調,對象狀態) 在System.Threading.ExecutionContext.Run(的ExecutionContext的ExecutionContext,ContextCallback回調,對象狀態)在System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult結果) 在System.Net.TlsStream.Write(Byte []緩衝區,Int32偏移量,Int32大小) 在System.Net.PooledStream.Write(字節[]緩衝區,Int32偏移量,Int32大小) 在System.Net.ConnectStream.WriteHeaders(布爾異步)

有沒有任何機會看到什麼可能是這個原因問題?

回答

17

添加請求之前,這一呼籲幫助我:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; 

所以,如果你使用的是HTTPS嘗試更改默認SecurityProtocol。

+0

這只是保存了我的一天..顯然還有網站在那裏運行SSL2.0。 – Grubsnik 2012-10-24 13:47:50

0

我不知道爲什麼(尤其是因爲它違背了我在這件事上看到的文檔),但我發現,忽略RequestLength的任務。相反,只需寫入請求流而不設置標題。

這還具有以下優勢:

using(Stream stm = req.GetRequestStream()) 
using(StreamWriter sw = new StreamWriter(stm)) 
{ 
    sw.Write(postData); 
} 

是我的腦海裏簡單地使用的情況下具有較大的優勢,其中的數據是更大一塊寫一塊,或來自類似的XMLWriter沿可以設置爲直接寫入正在討論的流。

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

+0

你先生是個天才,我欠你一杯啤酒。謝謝! – 2015-08-08 06:34:10

+3

你曾經買過那種啤酒嗎? – 2016-12-12 11:16:21

1

我的經歷是一樣elRobbo描述。我必須將Win2k3服務器升級到Win2k8。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 

只支持.NET 4.5,這是不支持WIN2K3 ...

0

這爲我工作:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 

我已經把該行創建Web請求之前。