Skip to main content
Ajuste na formatação
Fonte Link
Jeferson Almeida
  • 3,1mil
  • 1
  • 16
  • 37

Mensagem An error occurred while sending the request. Erro de segurança. StackTrace

   at System.Net.HttpWebRequest.GetResponse()

at TesteCriacaoSoapUnha.RequestWS.EnviaSefaz(XmlNode xml, String url, String metodo) in C:\Users\rober\Documents\Visual Studio 2017\Projects\TesteCriacaoSoapUnha\TesteCriacaoSoapUnha\RequestWS.cs:line 39

inner stacktrace

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at System.Net.Http.HttpClient.<FinishSendAsyncUnbuffered>d__59.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at System.Net.HttpWebRequest.d__194.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Net.HttpWebRequest.GetResponse()

outra inner

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

at System.Threading.Tasks.RendezvousAwaitable`1.GetResult() at System.Net.Http.WinHttpHandler.d__105.MoveNext()

Mensagem An error occurred while sending the request. Erro de segurança. StackTrace

   at System.Net.HttpWebRequest.GetResponse()    at TesteCriacaoSoapUnha.RequestWS.EnviaSefaz(XmlNode xml, String url,

String metodo) in C:\Users\rober\Documents\Visual Studio 2017\Projects\TesteCriacaoSoapUnha\TesteCriacaoSoapUnha\RequestWS.cs:line 39

inner stacktrace

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()    at

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at System.Net.Http.HttpClient.<FinishSendAsyncUnbuffered>d__59.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at System.Net.HttpWebRequest.d__194.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Net.HttpWebRequest.GetResponse()

outra inner

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()    at

System.Threading.Tasks.RendezvousAwaitable`1.GetResult() at System.Net.Http.WinHttpHandler.d__105.MoveNext()

Mensagem An error occurred while sending the request. Erro de segurança. StackTrace

   at System.Net.HttpWebRequest.GetResponse()

at TesteCriacaoSoapUnha.RequestWS.EnviaSefaz(XmlNode xml, String url, String metodo) in C:\Users\rober\Documents\Visual Studio 2017\Projects\TesteCriacaoSoapUnha\TesteCriacaoSoapUnha\RequestWS.cs:line 39

inner stacktrace

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at System.Net.Http.HttpClient.<FinishSendAsyncUnbuffered>d__59.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at System.Net.HttpWebRequest.d__194.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Net.HttpWebRequest.GetResponse()

outra inner

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

at System.Threading.Tasks.RendezvousAwaitable`1.GetResult() at System.Net.Http.WinHttpHandler.d__105.MoveNext()

Mensagem An error occurred while sending the request. Erro de segurança. StackTrace

   at System.Net.HttpWebRequest.GetResponse()    at TesteCriacaoSoapUnha.RequestWS.EnviaSefaz(XmlNode xml, String url,

String metodo) in C:\Users\rober\Documents\Visual Studio 2017\Projects\TesteCriacaoSoapUnha\TesteCriacaoSoapUnha\RequestWS.cs:line 39

inner stacktrace

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()    at

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at System.Net.Http.HttpClient.<FinishSendAsyncUnbuffered>d__59.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at System.Net.HttpWebRequest.d__194.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Net.HttpWebRequest.GetResponse()

outra inner

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()    at

System.Threading.Tasks.RendezvousAwaitable`1.GetResult() at System.Net.Http.WinHttpHandler.d__105.MoveNext()

Fonte Link

Consumir SOAP12 NF-e UF BA

Bom segue o código , no final posto o erro observação o mesmo código funciona para o Estado de GO (Goiás)

static void Main(string[] args)
    {
        try
        {

            var req = new RequestWS();

            var xmlDocument = new XmlDocument();
            //xmlDocument.LoadXml("<consStatServ versao=\"3.10\" xmlns=\"http://www.portalfiscal.inf.br/nfe\"><tpAmb>2</tpAmb><cUF>52</cUF><xServ>STATUS</xServ></consStatServ>");
            xmlDocument.LoadXml("<consStatServ versao=\"3.10\" xmlns=\"http://www.portalfiscal.inf.br/nfe\"><tpAmb>1</tpAmb><cUF>29</cUF><xServ>STATUS</xServ></consStatServ>");


            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;



            var resp = req.EnviaSefaz(xmlDocument, "https://nfe.sefaz.ba.gov.br/webservices/NfeStatusServico/NfeStatusServico.asmx", "http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico/nfeStatusServicoNF");
            //var resp = req.EnviaSefaz(xmlDocument, "https://homolog.sefaz.go.gov.br/nfe/services/v2/NfeStatusServico2?wsdl", "nfeStatusServicoNF2");
        }
        catch (Exception e)
        {
            System.Console.WriteLine(e);
            throw;
        }

Vejam que tenho até comentado a parte de UF GO

Classe RequestWS

public class RequestWS
{
    public string EnviaSefaz(XmlNode xml, string url, string metodo)
    {
        try
        {
            string XMLRetorno = string.Empty;
            string xmlSoap = new Envelopar().Construir(xml);

            Uri uri = new Uri(url);

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

            httpWR.ContentLength = Encoding.ASCII.GetBytes(xmlSoap).Length;

            httpWR.ClientCertificates.Add(new X509Certificate2(@"C:\Users\rober\Documents\Certificados\AGIL4 TECNOLOGIA LTDA  ME21025760000123.pfx", "agil4@123"));

            httpWR.ComposeContentType("application/soap+xml", Encoding.UTF8, metodo);

            httpWR.Method = "POST";

            Stream reqStream = httpWR.GetRequestStream();
            StreamWriter streamWriter = new StreamWriter(reqStream);
            streamWriter.Write(xmlSoap, 0, Encoding.ASCII.GetBytes(xmlSoap).Length);
            streamWriter.Close();

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

            XMLRetorno = streamReader.ReadToEnd();

            return XMLRetorno;
        }
        catch (WebException ex)
        {
            using (var stream = ex.Response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                Console.WriteLine(reader.ReadToEnd());
            }
            throw;
        }
    }
}

public static class ExtContentType
{
    internal static void ComposeContentType(this HttpWebRequest http, string contentType, Encoding encoding, string action)
    {
        if (encoding == null && action == null)
            http.ContentType = contentType;

        StringBuilder stringBuilder = new StringBuilder(contentType);

        if (encoding != null)
        {
            stringBuilder.Append("; charset=");
            stringBuilder.Append(encoding.WebName);
        }

        if (action != null)
        {
            stringBuilder.Append("; action=\"");
            stringBuilder.Append(action);
            stringBuilder.Append("\"");
        }

        http.ContentType = stringBuilder.ToString();
    }
}

Classe Envelopar

public class Envelopar
{
    public string Construir(XmlNode xml)
    {
        StringBuilder env = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");


        env.Append("<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
        env.Append("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
        env.Append("xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">");

        env.Append("<soap12:Header>");
        env.Append("<nfeCabecMsg xmlns=\"http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico\">"); // mudar namespace para goiás
        env.Append("<versaoDados>3.10</versaoDados>");
        env.Append("<cUF>29</cUF>"); // 52 para goiás
        env.Append("</nfeCabecMsg>");
        env.Append("</soap12:Header>");

        env.Append("<soap12:Body>");

        env.Append("<nfeDadosMsg xmlns=\"http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico\">"); // mudar namespace para goiás
        env.Append(xml.LastChild.OuterXml.ToString());
        env.Append("</nfeDadosMsg>");

        env.Append("</soap12:Body>");
        env.Append("</soap12:Envelope>");

        return env.ToString();
    }
}

Erro ocorrendo

O erro ocorre na linha

WebResponse webResponse = httpWR.GetResponse();

Mensagem An error occurred while sending the request. Erro de segurança. StackTrace

   at System.Net.HttpWebRequest.GetResponse()

at TesteCriacaoSoapUnha.RequestWS.EnviaSefaz(XmlNode xml, String url, String metodo) in C:\Users\rober\Documents\Visual Studio 2017\Projects\TesteCriacaoSoapUnha\TesteCriacaoSoapUnha\RequestWS.cs:line 39

inner stacktrace

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at System.Net.Http.HttpClient.<FinishSendAsyncUnbuffered>d__59.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at System.Net.HttpWebRequest.d__194.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Net.HttpWebRequest.GetResponse()

outra inner

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

at System.Threading.Tasks.RendezvousAwaitable`1.GetResult() at System.Net.Http.WinHttpHandler.d__105.MoveNext()