Consuming SOAP12 NF-e UF BA

0

Good follows the code, in the end put the error note the same code works for the GO state (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;
        }

See that I've even commented on the part of UF GO

RequestWS Class

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();
    }
}

Envelope Class

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();
    }
}

Error occurring

The error occurs on the line

WebResponse webResponse = httpWR.GetResponse();
  

Message An error occurred while sending the request. Error of   safety. StackTrace

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

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

     

inner stacktrace

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

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Task   task at   System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1.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.ConfiguredTaskAwaitable 1.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 ()

     

other inner

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

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

    
asked by anonymous 29.09.2017 / 15:47

1 answer

0

Resolved, with the help of a friend who owns this repository link has been resolved.

I simply switched the communication protocol to TLS (before I had placed TLS12)

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

This works as well

Use this code before executing the xml submission to the sefaz.

    
29.09.2017 / 16:52