How to issue the XML from NFe to SEFAZ WS using C #?

5

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

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

Last year in a forum, link I raised the following question:

The WS above has the class NfeReception2SoapClient. When we instantiate it, we get only the method nfeReceptionObject2 that accepts arguments for which it is not an XML string and returns an array of Object.

Dai I ask, how do I send the XML to SEFAZ? Anyone at least have any idea?

    
asked by anonymous 08.10.2014 / 15:34

1 answer

8

I do not use webservice calls using add reference, do not do this. It is a great job, and the Sefaz are not standardized, as 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;
}
    
08.10.2014 / 16:26