为什么服务器强行关闭连接?它对客户有什么期望?

时间:2014-12-29 22:17:01

标签: c# compact-framework windows-ce servicepoint servicepointmanager

尝试在我的本地网络上运行的服务器应用程序上从我在手持设备上运行的应用程序调用REST方法时,它失败并且" 无法为SSL / TLS建立安全通道... System.Net.Sockets.SocketException:远程主机强制关闭现有连接"

在.NET的更新/全功能版本中,您可以将此代码添加到客户端:

ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

...如图所示here接受服务器的响应。但是,在我的Windows CE / Compact Framework应用程序中,这是不可用的 - 虽然ServicePointManager是一个已知的数量," ServerCertificateValidationCallback不是。这是Windows.NET程序集apparently的一部分,在此项目中我无法使用。

所以,假设仍然有办法完成同样的事情(接受ssl-ified服务器的响应),在这种情况下如何做到这一点?

一个建议的解决方法是向我的客户端添加一个类,如下所示:

namespace HHS
{
    using System.Net;
    using System.Security.Cryptography.X509Certificates;

    class TrustAllCertificatesPolicy : ICertificatePolicy
    {
        public TrustAllCertificatesPolicy()
        {
        }

        public bool CheckValidationResult(ServicePoint sp, X509Certificate cert, WebRequest req, 
int problem)
        {
            return true;
        }
    }
}

...然后在app启动时调用它(例如在主窗体的Load()事件中):

private void frmMain_Load(object sender, EventArgs e)
{
    System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatesPolicy();
}

...但我仍然在我的日志文件中得到此异常:

Message: From FileXferREST.SendHTTPRequestNoCredentials(): Could not establish secure channel for SSL/TLS; Inner Ex: System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
   at System.Net.Sockets.Socket.ReceiveNoCheck(Byte[] buffer, Int32 index, Int32 request, SocketFlags socketFlags)
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Connection.System.Net.ISslDataTransport.Receive(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.SslConnectionState.ClientSideHandshake()
   at System.Net.SslConnectionState.PerformClientHandShake()
   at System.Net.Connection.connect(Object ignored)
   at System.Threading.ThreadPool.WorkItem.doWork(Object o)
   at System.Threading.Timer.ring()
; Stack Trace:    at System.Net.HttpWebRequest.finishGetRequestStream()
   at System.Net.HttpWebRequest.GetRequestStream()
   at HHS.FileXferREST.SendHTTPRequestNoCredentials(String uri, HttpMethods method, String data, String contentType)
. . .
BTW,TrustAllCertificatesPolicy' s(空)构造函数可能没什么问题,因为它是灰色的。

服务器期待客户端的确切含义是什么?为什么服务器强行关闭连接?

更新

在尝试确定我的客户端代码或服务器代码中是否存在问题时,我尝试通过邮递员点击网址,但得到此信息:

enter image description here

所以,我按照"在Chrome中导入SSL证书"链接以查看是否有帮助。

那[http://blog.getpostman.com/index.php/2014/01/28/using-self-signed-certificates-with-postman/]告诉我:

事实证明,有一个更好的解决方案来解决这个问题,并且它还避免了每次启动浏览器时都向Chrome添加例外。你就是这样做的:

1. Go to the root URL in your browser. For ex. https://localhost
2. Click on the lock icon on the top left in the URL bar.
. . .

但是,在Chrome中输入https://localhost后,我得到了#34;此网页不可用"

尝试http://localhost代替{" http"而不是" https"),OTOH,我得到了一个" IIS7"图片," welcome"在一系列(人类)语言中。

对于第2步("点击URL栏左上角的锁定图标。"),我看不到那样的事情;这就是我所看到的:

enter image description here

所以我注定了吗?

0 个答案:

没有答案