Link to home
Start Free TrialLog in
Avatar of bfeddish
bfeddishFlag for United States of America

asked on

"The request was aborted: Could not create SSL/TLS secure channel." in C# web service

I have a piece of code that works fine in a windows executable but when I move the same code to a web services it fails with:
"The request was aborted: Could not create SSL/TLS secure channel."
see the below code where I wrote "FAILS"

               ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                HttpWebRequest req1 = (HttpWebRequest)WebRequest.Create("https://thewebsite?wsdl");

                req1.ContentType = "text/xml; charset=\"utf-8\"";
                req1.Accept = "text/xml";
                req1.Method = "POST";

                System.Security.Cryptography.X509Certificates.X509Certificate2 x = new System.Security.Cryptography.X509Certificates.X509Certificate2("D:\\tlr\\TLR.crt", "");
                req1.ClientCertificates.Add(x);

                using (Stream stm = req1.GetRequestStream())
                {
                    using (StreamWriter stmw = new StreamWriter(stm))
                    {
                        stmw.Write(oRequest);
                    }
                }

                IAsyncResult asyncResult = req1.BeginGetResponse(null, null);

                asyncResult.AsyncWaitHandle.WaitOne();

                string soapResult = "";
                using (WebResponse webResponse = req1.EndGetResponse(asyncResult)) // FAILS!
                {
                    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                    {
                        soapResult = rd.ReadToEnd();
                    }
                }

                if (soapResult != "")
                {
                    // We don't get back allot from the hotel search so lets go get more info
                    hotelData = ParseOTA_HotelSearchRQXML(soapResult);

                    foreach (HotelSearchData hotelval in hotelData)
                    {
                        HotelSearchData ret = (GetBestWesternDetails(hotelval) );
                        hotelDataRet.Add(ret);
                    }
                }
Avatar of zephyr_hex (Megan)
zephyr_hex (Megan)
Flag of United States of America image

Are you running it on the same server?  I ran into a similar problem recently with Tls12 and found that it's not supported on older versions on Windows.
Avatar of bfeddish

ASKER

It is the exact same server, I just took the code out of the executable and placed it in the service.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.