WCF Error - Request cancelled: Failed to create secure channel for SSL/TLS

Asked

Viewed 737 times

0

I’m having a problem when it comes to using the HttpWebRequest, the first time I call the method to effect sending an NF-e to Sefaz’s Webservices it performs normally but the second time I try to send another NF-e it generates me the following error:

Request cancelled: Failed to create a secure channel for SSL/TLS.

More if I restart the WCF it performs the upload correctly.

Below is the code I’m using.

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

string XMLRetorno = string.Empty;

Uri uri = new Uri(url);

WebRequest webRequest = WebRequest.Create(uri);
HttpWebRequest httpWR = (HttpWebRequest)webRequest;

httpWR.Headers.Add("SOAPAction", metodo);
httpWR.ContentType = "application/soap+xml;charset=\"utf-8\"";
httpWR.Accept = "aplication/xml";
httpWR.Method = "POST";
httpWR.Timeout = Timeout.Infinite;
httpWR.KeepAlive = true;
httpWR.UseDefaultCredentials = true;
httpWR.Proxy = webProxy;

httpWR.ContentLength = Encoding.UTF8.GetBytes(xmlSoap).Length;
httpWR.ClientCertificates.Clear();
httpWR.ClientCertificates.Add(certificado);

Stream reqStream = httpWR.GetRequestStream();
reqStream.Write(Encoding.UTF8.GetBytes(xmlSoap), 0, Encoding.UTF8.GetBytes(xmlSoap).Length);
reqStream.Close();
reqStream.Dispose();

WebResponse webResponse = httpWR.GetResponse();
Stream respStream = webResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(respStream);

XMLRetorno = streamReader.ReadToEnd();
streamReader.Close();
streamReader.Dispose();

return XMLRetorno;
  • I’m not sure, but it might be that you’re keeping the connection open even after sending, try changing this flag httpWR.KeepAlive he’s like true place false and take a test.

  • @Lodi I put more now he’s giving all the time.

No answers

Browser other questions tagged

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