How to resolve the "Could not create SSL / TLS secure channel."

1

In my project, I need to communicate with a WebService and send data to it. But when I do the wrapping and sending, it presents the following error:

  

"Could not create SSL / TLS secure channel.".

Follow the code below:

Method Call:

string xml = @"<tjce:solicitacaoSelo xmlns:ds=""http://www.w3.org/2000/09/xmldsig#"" xmlns:tjce=""http://www.tjce.jus.br/selodigital/schemas"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:schemaLocation=""http://www.tjce.jus.br/selodigital/schemas xsSolicitacaoSelo_v1.0.xsd "">" +
          @"<cabecalho> <versao>1.11</versao>     <dataHora>20160913T03:54:09</dataHora>    <ambiente>2</ambiente>    <serventia>      <codigoServentia>000307</codigoServentia>    </serventia>  </cabecalho>  <solicitante>    <nomePessoa>CICERO MOZART MACHADO</nomePessoa>    <documento>      <tipoDocumento>1</tipoDocumento>      <numero>90002167595</numero>      <descricao>IDENTIDADE</descricao>      <orgaoEmissor>SSP-CE</orgaoEmissor>      <dataEmissao>1998-03-27</dataEmissao>    </documento>    <telefone>      <tipoTelefone>1</tipoTelefone>      <ddd>85</ddd>      <numero>32269240</numero>    </telefone>    <email>      <tipoEmail>1</tipoEmail>" +
          @"<enderecoEmail>[email protected]</enderecoEmail>    </email>  </solicitante>  <idSolicitacaoSelo>20</idSolicitacaoSelo>  <itens>    <itemSolicitacao>      <sequencial>1</sequencial>      <codigoSelo>        <codigo>1</codigo>      </codigoSelo>      <quantidade>1</quantidade>    </itemSolicitacao>  </itens></tjce:solicitacaoSelo>";

CSoap.ExecutaWebService("https://selodigital.tjce.jus.br/wsselodigital-homologacao/SolicitacaoSelo?wsdl", xml);
Solicitacao.solicitaSelos ws = new Solicitacao.solicitaSelos();

public class CSoap
{
    public static void ExecutaWebService(string _url, string xml)
    {
        HttpWebRequest request = CreateWebRequest(_url);
        XmlDocument soapEnvelopeXml = CreateSoapEnvelope(xml);

        using (Stream stream = request.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }


        using (WebResponse response = request.GetResponse())
        {
            using (StreamReader rd = new StreamReader(response.GetResponseStream()))
            {
                string soapResult = rd.ReadToEnd();
                Console.WriteLine(soapResult);
            }
        }
    }

    private static HttpWebRequest CreateWebRequest(string url)
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Headers.Add("SOAPAction:");
        webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        webRequest.Accept = "text/xml";
        webRequest.Method = "POST";
        return webRequest;
    }

    private static XmlDocument CreateSoapEnvelope(string xml)
    {
        XmlDocument soapEnvelop = new XmlDocument();
        soapEnvelop.LoadXml(@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/1999/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/1999/XMLSchema"">
                              <SOAP-ENV:Body>" + xml + @"</SOAP-ENV:Body></SOAP-ENV:Envelope>");
        return soapEnvelop;
    }
}
    
asked by anonymous 14.09.2016 / 15:23

2 answers

2

German, try entering the code below before sending the request, right after the envelope:

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

I hope I have helped, good luck.

    
19.09.2016 / 18:36
0

Use the following line BEFORE CREATING THE REQUEST :

ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;

After that the SSL / TLS service will be identified as secure and you can proceed with the request, so your code would look like this:

string xml = @"<tjce:solicitacaoSelo xmlns:ds=""http://www.w3.org/2000/09/xmldsig#"" xmlns:tjce=""http://www.tjce.jus.br/selodigital/schemas"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:schemaLocation=""http://www.tjce.jus.br/selodigital/schemas xsSolicitacaoSelo_v1.0.xsd "">" +
          @"<cabecalho> <versao>1.11</versao>     <dataHora>20160913T03:54:09</dataHora>    <ambiente>2</ambiente>    <serventia>      <codigoServentia>000307</codigoServentia>    </serventia>  </cabecalho>  <solicitante>    <nomePessoa>CICERO MOZART MACHADO</nomePessoa>    <documento>      <tipoDocumento>1</tipoDocumento>      <numero>90002167595</numero>      <descricao>IDENTIDADE</descricao>      <orgaoEmissor>SSP-CE</orgaoEmissor>      <dataEmissao>1998-03-27</dataEmissao>    </documento>    <telefone>      <tipoTelefone>1</tipoTelefone>      <ddd>85</ddd>      <numero>32269240</numero>    </telefone>    <email>      <tipoEmail>1</tipoEmail>" +
          @"<enderecoEmail>[email protected]</enderecoEmail>    </email>  </solicitante>  <idSolicitacaoSelo>20</idSolicitacaoSelo>  <itens>    <itemSolicitacao>      <sequencial>1</sequencial>      <codigoSelo>        <codigo>1</codigo>      </codigoSelo>      <quantidade>1</quantidade>    </itemSolicitacao>  </itens></tjce:solicitacaoSelo>";

CSoap.ExecutaWebService("https://selodigital.tjce.jus.br/wsselodigital-homologacao/SolicitacaoSelo?wsdl", xml);
Solicitacao.solicitaSelos ws = new Solicitacao.solicitaSelos();

public class CSoap
{
    public static void ExecutaWebService(string _url, string xml)
    {
        ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
        HttpWebRequest request = CreateWebRequest(_url);
        XmlDocument soapEnvelopeXml = CreateSoapEnvelope(xml);

        using (Stream stream = request.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }


        using (WebResponse response = request.GetResponse())
        {
            using (StreamReader rd = new StreamReader(response.GetResponseStream()))
            {
                string soapResult = rd.ReadToEnd();
                Console.WriteLine(soapResult);
            }
        }
    }

    private static HttpWebRequest CreateWebRequest(string url)
    {
        ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.Headers.Add("SOAPAction:");
        webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        webRequest.Accept = "text/xml";
        webRequest.Method = "POST";
        return webRequest;
    }

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

But it's worth noting that this is not indicated, because it can cause security vulnerabilities.

    
05.02.2018 / 13:23