【问题标题】:HTTPS Client/Server Communication Error: The underlying connection was closed: An unexpected error occurred on a send [duplicate]HTTPS客户端/服务器通信错误:底层连接已关闭:发送时发生意外错误[重复]
【发布时间】:2015-12-07 20:06:24
【问题描述】:

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

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

这是客户端的代码:

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);
            }
      }
 }

我已经尝试过这里提到的解决方案:C# System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send

我还更改了 MaxServicePointIdleTime 和 KeepAlive 设置。

【问题讨论】:

    标签: c# ssl https


    【解决方案1】:

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

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

    https://stackoverflow.com/questions/11403333/httplistener-with-https-support#=

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-02
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多