B
Barbi Rio
Guest
Hi,
I'm sending SOAP call with attached certificate and I'm getting the following error:The request was aborted: Could not create SSL/TLS secure channel
Here is my code:
public static void CallMyWebService()
{
try
{
var _url = "https://myurl.com";
var _action = "https://myurl.com/myAction";
XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11;
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
asyncResult.AsyncWaitHandle.WaitOne();
string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
Console.Write(soapResult);
}
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
}
private static HttpWebRequest CreateWebRequest(string url, string action)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", action);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
string certificatePath2 = "myCertificate.cer";
X509Certificate certificate2 = new X509Certificate(certificatePath2);
if (certificate2 == null)
{
certificate2 = X509Certificate.CreateFromCertFile(certificatePath2);
}
webRequest.ClientCertificates.Add(certificate2);
return webRequest;
}
private static XmlDocument CreateSoapEnvelope()
{
XmlDocument soapEnvelopeDocument = new XmlDocument();
try
{
soapEnvelopeDocument.Load("XMLFile1.xml");
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
return soapEnvelopeDocument ;
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
try
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
}
I put both certificates in the bin folder and also in the project folder. Both of the root certificates are installed.
Please help
Continue reading...
I'm sending SOAP call with attached certificate and I'm getting the following error:The request was aborted: Could not create SSL/TLS secure channel
Here is my code:
public static void CallMyWebService()
{
try
{
var _url = "https://myurl.com";
var _action = "https://myurl.com/myAction";
XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11;
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
asyncResult.AsyncWaitHandle.WaitOne();
string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
Console.Write(soapResult);
}
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
}
private static HttpWebRequest CreateWebRequest(string url, string action)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", action);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
string certificatePath2 = "myCertificate.cer";
X509Certificate certificate2 = new X509Certificate(certificatePath2);
if (certificate2 == null)
{
certificate2 = X509Certificate.CreateFromCertFile(certificatePath2);
}
webRequest.ClientCertificates.Add(certificate2);
return webRequest;
}
private static XmlDocument CreateSoapEnvelope()
{
XmlDocument soapEnvelopeDocument = new XmlDocument();
try
{
soapEnvelopeDocument.Load("XMLFile1.xml");
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
return soapEnvelopeDocument ;
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
try
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
}
I put both certificates in the bin folder and also in the project folder. Both of the root certificates are installed.
Please help
Continue reading...