本文介绍了HTTPS客户端/服务器通信错误:基础连接已关闭:发送时发生意外错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将端口443上的XML发送到HTTPS侦听器时,我收到以下错误。侦听器(C#控制台应用程序)只响应客户端发送的相同XML(一个简单的win表单应用程序)。如果我将侦听器应用程序更改为HTTP而不是HTTPS,则应用程序可以正常通信。理想情况下,此应用程序将在网络上运行。我已经尝试在另一台服务器上托管监听器应用程序,因此客户端必须访问网络并在与客户端相同的计算机上加载监听器。我正在尝试消除任何编码问题,因此我可以将其交给我们的网络人员,但我觉得他们需要指明方向。

I'm receiving the following error when sending XML across to a HTTPS listener on port 443. The Listener (C# Console Application) simply responds with the same XML it is being sent by the client (a simple win forms application). If I change the listener application to HTTP instead of HTTPS, the applications communicate fine. Ideally this application will run across a network. I've tried both hosting the listener application on a another server so the client has to hit the network and loading the listener on the same machine as the client. I'm trying to eliminate any coding issues so I can hand this off to our network people, but I feel like they'll need a direction to be pointed in.

内部异常:{无法从传输连接读取数据:远程主机强行关闭现有连接。}

Inner Exception: {"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."}

以下是客户端的代码:

Here is the code for the Client:

xmlOutBox.Text = "";
        ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
        ServicePointManager.MaxServicePointIdleTime = 1;
        System.Net.WebRequest req = null;
        System.Net.WebResponse rsp = null;
        try
        {
            string uri = urlBox.Text;
            req = System.Net.HttpWebRequest.Create(uri);
            ((HttpWebRequest)req).KeepAlive = false;
            req.Method = "POST";
            req.ContentType = "text/xml";
            System.IO.StreamWriter writer =
        new System.IO.StreamWriter(req.GetRequestStream());
            writer.WriteLine(xmlInBox.Text);
            writer.Close();
            using (var response = (HttpWebResponse)req.GetResponse())
            {
                var respon = response.GetResponseStream();
                using (var reader = new System.IO.StreamReader(respon, Encoding.UTF8))
                {
                    string responseText = reader.ReadToEnd();
                    xmlOutBox.Text = responseText;
                }
            }
        }

以下是监听器代码:

 static void Main(string[] args)
    {
        HttpListener listener = new HttpListener();
        listener.Prefixes.Add("https://*:443/");
        listener.Start();
        Console.WriteLine("Listening...");

        for(;;)
        {
            HttpListenerContext ctx = listener.GetContext();
            new Thread(new Worker(ctx).ProcessRequest).Start();
        }
    }

 class Worker
    {
        private HttpListenerContext context;

        public Worker(HttpListenerContext context)
        {
            this.context = context;
        }

        public void ProcessRequest()
        {
            string text;
            using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding))
            {
                text = reader.ReadToEnd();
                Console.WriteLine(text);
            }
      }
 }

我试过这个解决方案这里提到:

I've tried the solutions mentioned here: C# System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send

我还更改了MaxServicePointIdleTime和KeepAlive设置。

I have also altered the MaxServicePointIdleTime and KeepAlive settings.

推荐答案

代码方面,OP中显示的内容没有任何问题。问题是使用没有签名证书的HTTPS。在我创建自签名证书并将其绑定到我正在使用的端口(端口443)后,应用程序很乐意接受HTTPS请求。

Code wise, there is nothing wrong with what's shown in the OP. The problem is with using HTTPS without a signed certificate. After I created a self signed certificate and bound it to the port I was using (port 443), the application gladly accepted HTTPS requests.

此处的建议将显示如何一步一步纠正问题:

The recommendations here will show how to correct the issue step by step:

这篇关于HTTPS客户端/服务器通信错误:基础连接已关闭:发送时发生意外错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 12:29