How to resolve "Could not create SSL/TLS Secure Channel." error?

Asked

Viewed 7,845 times

2

In my project, I need to communicate with a Webservice and send data to it. However when I do the envelopment and sent, it shows the following error:

"Could not create SSL/TLS Secure Channel.".

Follows code:

Call of the Metodo:

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;
    }
}
  • You are using digital certificate?

  • Yes, and I’ve used up the Webservice methods, but I can’t access the XML upload. And from what I’ve been informing myself, it doesn’t require authentication, SSL or TLS.

  • Usually this error occurs because there are problems in xml. see if tjce has an xml validation service...

  • I believe that is why, because XML is validated

2 answers

3

Germano, try to enter the code below before sending the request, right after the envelopment:

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

I hope I helped, good luck.

  • Works for me, tanks!!!!

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 safe and you can proceed with the request, then 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 remember that this is not indicated because it can cause security vulnerabilities.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.