c# - 发送时发生意外错误。传输流中的 EOF 或 0 字节

标签 c# asp.net asp.net-mvc httpwebrequest

我使用以下代码向 https 站点发出请求:

 webRequest = (HttpWebRequest)WebRequest.Create(new Uri(urlDestination));
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = bytes.Length;

            Stream writer = webRequest.GetRequestStream();
            writer.Write(bytes, 0, bytes.Length);
            if (writer != null)
            {
                writer.Close();
            }

            response = new StreamReader(webRequest.GetResponse().GetResponseStream()).ReadToEnd();

并得到这个错误:

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream.
   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, Boolean ignoreSyncCtx)
   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)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
   at System.Net.HttpWebRequest.GetRequestStream()
   at Nop.Proxy.send_sync_post.process(NameValueCollection parametersReceived)
   at Nop.Proxy.Includes.Page_Load(Object sender, EventArgs e)

我在网上检查了有关错误的信息后,我找到了这个解决方案但没有奏效:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

在与供应商联系后,他说他遇到了这个错误

SSL_do_handshake() failed (SSL: error:1408A0C1:SSL routines:SSL3_GET_CLIENT_HELLO:no shared cipher) while SSL handshaking

我知道错误来 self ,但不知道如何请求跳过错误。

最佳答案

ssl3 声明确实解决了我的问题,但我遇到了另一个不适用于我但对你适用的建议,因为你有一个内容长度声明。

建议删除该声明。

我的工作代码如下:

//Create WebRequest
Uri JsonUri = new Uri(new Uri(JIRAInstanceUri), ServiceUri);
WebRequest RestWebRequest = WebRequest.Create(JsonUri);
RestWebRequest.Method = "GET";
RestWebRequest.ContentType = "application/json";
//Add proxy to WebRequest
ProxyServer.BypassArrayList.Add(JsonUri);
RestWebRequest.Proxy = ProxyServer;
//Setup authorization
string Base64Credentials = GetEncodedCredentials(UserName, Password);
RestWebRequest.Headers["Authorization"] = "Basic " + Base64Credentials;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

关于c# - 发送时发生意外错误。传输流中的 EOF 或 0 字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16348393/

相关文章:

c# - 我可以从 NHibernate 获取底层连接和事务对象吗?

javascript - 如何在重定向之前打开新选项卡

c# - 从内存 ASP.Net 加载 PDF

javascript - 在asp.net mvc中将json数据渲染到网格中

c# - 我的 Controller 方法是异步执行的吗?

C# 委托(delegate)和线程!

c# - 如何在我自己的 Mono (c#) 项目中使用 SharpSVN?

c# - 如何在 asp.net 中编辑数据集列值

javascript - JQuery/dirty forms/window.onbeforeunload 仅在链接选择后触发

asp.net - LabelFor + EditorFor 在同一行?