0

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

Here is the listener code:

 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

I have also altered the MaxServicePointIdleTime and KeepAlive settings.

Community
  • 1
  • 1
Rafiki
  • 550
  • 1
  • 5
  • 21

1 Answers1

0

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://stackoverflow.com/questions/11403333/httplistener-with-https-support#=

Community
  • 1
  • 1
Rafiki
  • 550
  • 1
  • 5
  • 21