0
respostas

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

Se alguém puder me ajudar agradeço. Estou tentando efetuar um envio de um XML para o WebService da prefeitura de Maringá, o problema é que ocorre o erro "The request was aborted: Could not create SSL/TLS secure channel."

Estou utilizando Windows Server 2008 R2 Enterprise, abaixo segue o código q estou utilizando.

    public void TesteNFSe()
    {
        string _result = string.Empty;

        string _xml = System.IO.File.ReadAllText(@"C:\Projects\Skill\POC\NFSeConnection\NFSeConnection\XML\MaringaEnvio.XML");

        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://nfse-ws.ecity.maringa.pr.gov.br/v2.01");

        webRequest.Method = "POST";
        webRequest.ClientCertificates.Add(GetCertificado());
        ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate X509certificate, X509Chain chain, System.Net.Security.SslPolicyErrors errors)
        {
            return true;
        };
        webRequest.ServicePoint.Expect100Continue = true;
        webRequest.KeepAlive = false;
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;

        string postData = _xml.ToString();
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        webRequest.ContentLength = byteArray.Length;
        Stream dataStream = webRequest.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        using (WebResponse response = webRequest.GetResponse())
        {
            using (StreamReader rd = new StreamReader(response.GetResponseStream()))
            {
                _result = rd.ReadToEnd();
            }
        }
    }

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software