Consume SOAP12 NF-e UF BA

Asked

Viewed 1,076 times

0

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

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

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

Error occurring

Error occurs on 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 metodo) in C: Users Rober Documents Visual Studio 2017 Projects Testcreationsput Testcreationsput 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()

another Inner

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

System.Threading.Tasks.Rendezvousawaitable`1.Getresult() at System.Net.Http.WinHttpHandler.d__105.Movenext()

  • Instead of doing everything by hand, add a reference to the service from wsdl.

  • My problem is that I haven’t specified everything here, but I’m doing it by hand because I need it for . net standard is . net framework. I am a developer of an NF-e emission repository and I want to make this available to everyone who uses it.

1 answer

0


Solved, with the help of a friend who owns this repository https://github.com/ACBrNet/ACBr.Net.Sat was resolved.

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

Servicepointmanager.Securityprotocol = Securityprotocoltype.Tls | Securityprotocoltype.Tls11 | Securityprotocoltype.Tls12;

That’s how it works too

Use this code before uploading xml to Sefaz.

Browser other questions tagged

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