代码之家  ›  专栏  ›  技术社区  ›  Task

如何像HttpWebRequest那样使用TcpClient实现HTTPS?

  •  2
  • Task  · 技术社区  · 14 年前

    通过使用浏览器或HttpWebRequest,我对该IP使用HTTPS没有问题。

    我已经创建了一个测试程序来将我的问题缩小到它的基本本质,您可以在这里查看它,如果您想: TestViaTcp

    {"Authentication failed because the remote party has closed the transport stream."}
        [System.IO.IOException]: {"Authentication failed because the remote party has closed the transport stream."}
        Data: {System.Collections.ListDictionaryInternal}
        HelpLink: null
        InnerException: null
        Message: "Authentication failed because the remote party has closed the transport stream."
        Source: "System"
        TargetSite: {Void StartReadFrame(Byte[], Int32, System.Net.AsyncProtocolRequest)}
    

    下面是附加的堆栈跟踪:

       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.Security.SslStream.AuthenticateAsClient(String targetHost, X509CertificateCollection clientCertificates, SslProtocols enabledSslProtocols, Boolean checkCertificateRevocation)
       at DeriveClassNameSpace.Services.Web.TcpMessaging.TestViaTcp(IPEndPoint endpoint, String auth, Boolean useSSL)
    

    关于问题所在的最大暗示可能是“起作用,不起作用,起作用,不起作用”的模式,是什么导致了这种情况?我能做些什么来避免我遇到的IOException?当我使用HttpWebRequest处理HTTPS时,有没有什么方法可以让我看到“总是工作”的行为?

    注意:我正在与之通信的服务器可以配置它监听的端口和它期望的协议,但在其他方面是完全不可修改的。

    另请注意:我已经读到.NET3.5在SP1之前的SslStream有这个特殊的问题,但是我有SP1并且我的程序是针对3.5构建的,所以我假设这不是我在这里遇到的“已知错误”。

    1 回复  |  直到 14 年前
        1
  •  3
  •   Glorfindel Doug L.    4 年前

    你不知道吗,

    以下是相关文档: jpsanders blog entry

    重要的是:

    所以我把我接受的协议改为:(排除TLS的可能性)

    SslProtocols protocol = SslProtocols.Ssl2 | SslProtocols.Ssl3;
    


    我允许TLS,所以它首先尝试,而服务器不支持它,所以流被关闭。下一次使用Ssl2或Ssl3时一切正常。或者类似的。它起作用了,我是一只快乐的熊猫。