How to send Nfe XML to SEFAZ WS using C#?

Asked

Viewed 11,899 times

5

I have been doing a lot of research, reading several source codes of other languages and never found answer to my question, which method of SEFAZ web services I should call?

I’m working with this WS http://nfe.fazenda.mg.gov.br/nfe2/services/NfeRetRecepcao2?wsdl

Last year on a forum, http://social.msdn.microsoft.com/forums/vstudio/pt-BR/c75142b6-ad8a-43ee-9113-b2e9e9353b1b/acessando-webservice-da-nfe-da-sefaz I raised the following question:

The WS above has the class Nferecepcao2soapclient. At instance it is obtained only the nfeRecepcaoLote2 method that accepts arguments by which it is not an XML string and returns an Object array.

So I ask, how do I send XML to SEFAZ? Does anyone at least have any notion?

  • There is the Uninfe project, it has the source code, maybe it helps your case.

1 answer

8


I don’t use the webservice calls using the add Ference, don’t do that. It is a great job, and the Sefaz are not standardized, because it has been that it is different.

public static String soapXmlStatusServico(string cUF, int ambiente)
{
    String result = String.Empty;
    MemoryStream stream = new MemoryStream(); // The writer closes this for us

    using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8))
    {
        writer.WriteStartDocument();
        writer.WriteStartElement("soap:Envelope");
        writer.WriteAttributeString("xmlns:soap", "http://www.w3.org/2003/05/soap-envelope");
        writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        writer.WriteAttributeString("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
        writer.WriteStartElement("soap:Header");
        writer.WriteStartElement("nfeCabecMsg");
        writer.WriteAttributeString("xmlns", "http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico2");
        writer.WriteElementString("cUF", cUF);
        writer.WriteElementString("versaoDados", "3.10");
        writer.WriteEndElement();
        writer.WriteEndElement();
        writer.WriteStartElement("soap:Body");

        writer.WriteStartElement("nfeDadosMsg");
        writer.WriteAttributeString("xmlns", "http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico2");
        writer.WriteStartElement("consStatServ");
        writer.WriteAttributeString("xmlns", "http://www.portalfiscal.inf.br/nfe");
        writer.WriteAttributeString("versao", "3.10");
        writer.WriteElementString("tpAmb", ambiente.ToString());
        writer.WriteElementString("cUF", cUF);
        writer.WriteElementString("xServ", "STATUS");
        writer.WriteEndElement();
        writer.WriteEndElement();

        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Flush();
        writer.Flush();

        StreamReader reader = new StreamReader(stream, Encoding.UTF8, true);
        stream.Seek(0, SeekOrigin.Begin);

        result += reader.ReadToEnd();
    }

    return result;
}

private CookieContainer cookies = new CookieContainer();

public string RequestWebService(string wsURL, string param, string action, X509Certificate2 certificado)
{
    Uri urlpost = new Uri(wsURL);
    HttpWebRequest httpPostConsultaNFe = (HttpWebRequest)HttpWebRequest.Create(urlpost);

    string postConsultaComParametros = param;
    byte[] buffer2 = Encoding.ASCII.GetBytes(postConsultaComParametros);

    httpPostConsultaNFe.CookieContainer = cookies;
    httpPostConsultaNFe.Timeout = 300000;
    httpPostConsultaNFe.ContentType = "application/soap+xml; charset=utf-8; action=" + action;
    httpPostConsultaNFe.Method = "POST";
    httpPostConsultaNFe.ClientCertificates.Add(certificado);
    httpPostConsultaNFe.ContentLength = buffer2.Length;    

    Stream PostData = httpPostConsultaNFe.GetRequestStream();
    PostData.Write(buffer2, 0, buffer2.Length);
    PostData.Close();

    HttpWebResponse responsePost = (HttpWebResponse)httpPostConsultaNFe.GetResponse();
    Stream istreamPost = responsePost.GetResponseStream();
    StreamReader strRespotaUrlConsultaNFe = new StreamReader(istreamPost, System.Text.Encoding.ASCII);

    return strRespotaUrlConsultaNFe.ReadToEnd();
} 

public string NFeStatusServico(string xml, int cUF, string CnpjTransmissor, bool EPEC, X509Certificate2 certificado)
{
    string Retorno = string.Empty;
    try
    {
        string action = "http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico2/nfeStatusServicoNF2";

        #region "ADDRESS"

         string am = Properties.Settings.Default.am_hom_statusServico;
         string ba = Properties.Settings.Default.ba_hom_statusServico;
         string ce = Properties.Settings.Default.ce_hom_statusServico;
         string go = Properties.Settings.Default.go_hom_statusServico;
         string mg = Properties.Settings.Default.mg_hom_statusServico;
         string mt = Properties.Settings.Default.mt_hom_statusServico;
         string ms = Properties.Settings.Default.ms_hom_statusServico;
         string pe = Properties.Settings.Default.pe_hom_statusServico;
         string pr = Properties.Settings.Default.pr_hom_statusServico;
         string rs = Properties.Settings.Default.rs_hom_statusServico;
         string sp = Properties.Settings.Default.sp_hom_statusServico;
         string svan = Properties.Settings.Default.svan_hom_statusServico;
         string svrs = Properties.Settings.Default.svrs_hom_statusServico;
         string svcan = Properties.Settings.Default.svcan_hom_statusServico;
         string svcrs = Properties.Settings.Default.svcrs_hom_statusServico;

        #endregion

        SefazWebRequest request = new SefazWebRequest();

        #region "UF AUTORIZADORAS"
        switch (cUF)
        {
            case (int)
                Estados.AM:
                    if(EPEC)
                    {
                        //Envio Emissao Previa de Entrada em contingencia
                    }
                    else
                    {
                        Retorno = request.RequestWebService(am, xml, action, certificado);
                    }
                    break;
            case (int)Estados.BA:
                    if (EPEC)
                    {
                        //Envio Emissao Previa de Entrada em contingencia
                    }
                    else
                    {
                        Retorno = request.RequestWebService(ba, xml, action, certificado);
                    }
                    break;
            case (int)Estados.CE:
                    if (EPEC)
                    {
                        //Envio Emissao Previa de Entrada em contingencia
                    }
                    else
                    {
                        Retorno = request.RequestWebService(ce, xml, action, certificado);
                    }
                    break;
            case (int)Estados.GO:
                    if (EPEC)
                    {
                        //Envio Emissao Previa de Entrada em contingencia
                    }
                    else
                    {
                        Retorno = request.RequestWebService(go, xml, action, certificado);
                    }
                    break;
            case (int)Estados.MG:
                    if (EPEC)
                    {
                        //Envio Emissao Previa de Entrada em contingencia
                    }
                    else
                    {
                        Retorno = request.RequestWebService(mg, xml, action, certificado);
                    }
                    break;
            case (int)Estados.MS:
                    if (EPEC)
                    {
                        //Envio Emissao Previa de Entrada em contingencia
                    }
                    else
                    {
                        Retorno = request.RequestWebService(ms, xml, action, certificado);
                    }
                    break;
            case (int)Estados.MT:
                    if (EPEC)
                    {
                        //Envio Emissao Previa de Entrada em contingencia
                    }
                    else
                    {
                        Retorno = request.RequestWebService(mt, xml, action, certificado);
                    }
                    break;
            case (int)Estados.PE:
                    if (EPEC)
                    {
                        //Envio Emissao Previa de Entrada em contingencia
                    }
                    else
                    {
                        Retorno = request.RequestWebService(pe, xml, action, certificado);
                    }
                    break;
            case (int)Estados.PR:
                    if (EPEC)
                    {
                        //Envio Emissao Previa de Entrada em contingencia
                    }
                    else
                    {
                        Retorno = request.RequestWebService(pr, xml, action, certificado);
                    }
                    break;
            case (int)Estados.RS:
                    if (EPEC)
                    {
                        //Envio Emissao Previa de Entrada em contingencia
                    }
                    else
                    {
                        Retorno = request.RequestWebService(rs, xml, action, certificado);
                    }
                    break;
            case (int)Estados.SP:
                    if (EPEC)
                    {
                        //Envio Emissao Previa de Entrada em contingencia
                    }
                    else
                    {
                        Retorno = request.RequestWebService(sp, xml, action, certificado);
                    }
                    break;
        }
        #endregion

        #region "SVRS - SEFAZ VIRTUAL RIO GRANDE DO SUL = AC, AL, AP, DF, ES, PB, RJ, RN, RO, RR, SC, SE, TO "

        if (cUF == (int)Estados.AC ||
            cUF == (int)Estados.AL ||
            cUF == (int)Estados.AP ||
            cUF == (int)Estados.DF ||
            cUF == (int)Estados.ES ||
            cUF == (int)Estados.PB ||
            cUF == (int)Estados.RJ ||
            cUF == (int)Estados.RN ||
            cUF == (int)Estados.RO ||
            cUF == (int)Estados.RR ||
            cUF == (int)Estados.SC ||
            cUF == (int)Estados.SE ||
            cUF == (int)Estados.TO)
        {
            if (EPEC)
            {    
            }
            else
            {
                Retorno = request.RequestWebService(svrs, xml, action, certificado);
            }
        }

        #endregion

        #region "SEFAZ VIRTUAL AMBIENTE NACIONAL = MA, PA, PI"

        if (cUF == (int)Estados.MA ||
            cUF == (int)Estados.PA ||
            cUF == (int)Estados.PI)
        {
            if (EPEC)
            {    
            }
            else
            {
                Retorno = request.RequestWebService(svan, xml, action, certificado);
            }
        }

        #endregion

    }   
    catch(Exception ex)
    {
        eventos.AppLog("NFeStatusServico", ex.Message + "\r\n" + ex.StackTrace, EventLogEntryType.Error);
    }
    return Retorno;
}
  • I was doing just this, I would never imagine I shouldn’t use this way.

  • 1

    http://answall.com/questions/45430/como-enviar-o-xml-da-nfe-consumindo-o-webservice-da-sefaz

Browser other questions tagged

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