Link to home
Start Free TrialLog in
Avatar of mathew_s
mathew_s

asked on

The request was aborted: Could not create SSL/TLS secure channel

I have created a windows service and installed a certificate on my local machine. I am sending an XML query to the remote server and expecting a response back. I am getting an error: The request was aborted: Could not create SSL/TLS secure channel.

The error occurs on the following line:
            using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))

Note  that I have replaced text that is sensitive with blah.
Any ideas?

        public static void CallWebService()
        {
            var _url = "https://blah";
            var _action = "https://blah";


            XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
            HttpWebRequest webRequest = CreateWebRequest(_url, _action);
            InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);



            // begin async call to web request.
            IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);


            // suspend this thread until call is complete. 
            asyncResult.AsyncWaitHandle.WaitOne();


            // get the response from the completed web request.
            string soapResult;

            using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult)) //error occurs here!!
            {
                using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                {
                    soapResult = rd.ReadToEnd();
                }
                Console.Write(soapResult);
                Console.Read();
            }

        }


        private static HttpWebRequest CreateWebRequest(string url, string action)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Headers.Add("SOAPAction", action);
            webRequest.ContentType = "text/xml";
            //webRequest.ContentLength = ddd;
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";

            //create the proxy
            ICredentials credentials = new NetworkCredential("Blah", "");
            IWebProxy webProxy = new WebProxy("http://blah.com:9090", true);
            webProxy.Credentials = credentials;
            webRequest.Proxy = webProxy;

            return webRequest;
        }

        private static XmlDocument CreateSoapEnvelope()
        {
            XmlDocument soapEnvelop = new XmlDocument();
            soapEnvelop.LoadXml(@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/1999/XMLSchema""><SOAP-ENV:Body><QueryRequest><Blah><All/></Blah></QueryRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>");
            return soapEnvelop;
        }

        private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
        {
            using (Stream stream = webRequest.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }
        }

Open in new window

Avatar of mathew_s
mathew_s

ASKER

I modified the createwebrequest method to but still getting same error assuming it is a cerficate issue. It is not even connecting to the remote server:

        private static HttpWebRequest CreateWebRequest(string url, string action)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Headers.Add("SOAPAction", action);
            webRequest.ContentType = "text/xml";
            //webRequest.ContentLength = ddd;
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";

            //create the proxy
            ICredentials credentials = new NetworkCredential("Blah", "");
            IWebProxy webProxy = new WebProxy("http://blah.com:9090", true);
            webProxy.Credentials = credentials;
            webRequest.Proxy = webProxy;

            X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadOnly);
            X509Certificate2Collection col = store.Certificates.Find(X509FindType.FindBySubjectKeyIdentifier, "xxx", true);
            webRequest.ClientCertificates.Add(col[0]);
            store.Close();

            return webRequest;
        }
ASKER CERTIFIED SOLUTION
Avatar of mathew_s
mathew_s

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial