Error 500 when consuming Nfedistribuicaodfe

Asked

Viewed 2,325 times

2

I’m trying to consume the NFE download service on the farm:

https://www1.nfe.fazenda.gov.br/NFeDistribuicaoDFe/NFeDistribuicaoDFe.asmx

Specification

http://www.nfe.fazenda.gov.br/portal/exibirArquivo.aspx?conteudo=2Z%20kcHOdpHs=

Using .Net, C#, MVC

but I have obtained the following error :

Remote server returned an error: (500) Internal Server Error.

The message I’ve been sending is :

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <nfeDistDFeInteresse>
            <nfeDadosMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NFeDistribuicaoDFe">
                <distDFeInt versao="1.00" xmlns="http://www.portalfiscal.inf.br/nfe">
                    <tpAmb>1</tpAmb>
                    <cUFAutor>31</cUFAutor>
                    <CNPJ>[CNPJ DA EMPRESA]</CNPJ>
                    <distNSU>
                        <ultNSU>000000000000000</ultNSU>
                    </distNSU>
                </distDFeInt>
            </nfeDadosMsg>
        </nfeDistDFeInteresse>
    </soap:Body>
</soap:Envelope>

I removed all spaces, consumption other services successfully

request.Method = "POST";
request.ContentType = "text/xml";
request.ContentLength = byteData.Length;

The certificate I’m sure is correct too.

Rebound that I can consume other services.

WSDL of the service

<wsdl:definitions xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://www.portalfiscal.inf.br/nfe/wsdl/NFeDistribuicaoDFe" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://www.portalfiscal.inf.br/nfe/wsdl/NFeDistribuicaoDFe">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://www.portalfiscal.inf.br/nfe/wsdl/NFeDistribuicaoDFe">
<s:element name="nfeDistDFeInteresse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="nfeDadosMsg">
<s:complexType mixed="true">
<s:sequence>
<s:any/>
</s:sequence>
</s:complexType>
</s:element>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="nfeDistDFeInteresseResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="nfeDistDFeInteresseResult">
<s:complexType mixed="true">
<s:sequence>
<s:any/>
</s:sequence>
</s:complexType>
</s:element>
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:message name="nfeDistDFeInteresseSoapIn">
<wsdl:part name="parameters" element="tns:nfeDistDFeInteresse"/>
</wsdl:message>
<wsdl:message name="nfeDistDFeInteresseSoapOut">
<wsdl:part name="parameters" element="tns:nfeDistDFeInteresseResponse"/>
</wsdl:message>
<wsdl:portType name="NFeDistribuicaoDFeSoap">
<wsdl:operation name="nfeDistDFeInteresse">
<wsdl:input message="tns:nfeDistDFeInteresseSoapIn"/>
<wsdl:output message="tns:nfeDistDFeInteresseSoapOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="NFeDistribuicaoDFeSoap" type="tns:NFeDistribuicaoDFeSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="nfeDistDFeInteresse">
<soap:operation soapAction="http://www.portalfiscal.inf.br/nfe/wsdl/NFeDistribuicaoDFe/nfeDistDFeInteresse" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="NFeDistribuicaoDFeSoap12" type="tns:NFeDistribuicaoDFeSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="nfeDistDFeInteresse">
<soap12:operation soapAction="http://www.portalfiscal.inf.br/nfe/wsdl/NFeDistribuicaoDFe/nfeDistDFeInteresse" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="NFeDistribuicaoDFe">
<wsdl:port name="NFeDistribuicaoDFeSoap" binding="tns:NFeDistribuicaoDFeSoap">
<soap:address location="https://www1.nfe.fazenda.gov.br/NFeDistribuicaoDFe/NFeDistribuicaoDFe.asmx"/>
</wsdl:port>
<wsdl:port name="NFeDistribuicaoDFeSoap12" binding="tns:NFeDistribuicaoDFeSoap12">
<soap12:address location="https://www1.nfe.fazenda.gov.br/NFeDistribuicaoDFe/NFeDistribuicaoDFe.asmx"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

2 answers

0

following this tutorial I made class below these days ta working http://pmarco777.blogspot.com.br/2015/01/download-da-nota-fiscal-eletronica-nfe.html

/// <summary>
        /// Classe download NFe versaodados 3.10
        /// </summary>
        public class NFe
        {
            private string _serialCertificado;
            private RecepcaoEvento.RecepcaoEventoSoapClient _nfeRecepcao;
            private NfeDownload.NfeDownloadNFSoapClient _nfeDonwload;
            private string[] chaveAcesso;
            private string cnpj;
            private string codigoUf;
            private string path;

            public NFe(string[] chaveAcesso, string cnpj, string codigoUf, string path)
            {
                _nfeRecepcao = new RecepcaoEvento.RecepcaoEventoSoapClient();
                _nfeDonwload = new NfeDownload.NfeDownloadNFSoapClient();
                this.chaveAcesso = chaveAcesso;
                this.path = path;
                this.codigoUf = codigoUf;
                this.cnpj = cnpj;
            }

            public void ConsultaNfe()
            {
                _nfeRecepcao.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySerialNumber, GetSerialCertificado());
                var xml = MontarXmlConsulta(chaveAcesso);
                var nfeCab = new RecepcaoEvento.nfeCabecMsg() { cUF = codigoUf, versaoDados = "1.00" };
                var resp = _nfeRecepcao.nfeRecepcaoEvento(nfeCab, xml);
                var fileResp = path + chaveAcesso[0] + ".xml";
                var fileReq = path + chaveAcesso[0] + ".xml";
                File.WriteAllText(fileReq, xml.OuterXml);
                File.WriteAllText(fileResp, resp.OuterXml);
                DownloadNFE();
            }

            public void DownloadNFE()
            {
                var xml = MontarXmlDownload(chaveAcesso);
                var NFe_Cab = new NfeDownload.nfeCabecMsg() { cUF = codigoUf, versaoDados = "1.00" };
                var fileResp = path + chaveAcesso[0] + ".xml";
                var fileReq = path + chaveAcesso[0] + ".xml";
                _nfeDonwload.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySerialNumber, GetSerialCertificado());
                var resp = _nfeDonwload.nfeDownloadNF(NFe_Cab, xml);
                File.WriteAllText(fileReq, xml.OuterXml);
                File.WriteAllText(fileResp, resp.OuterXml);
                //System.Diagnostics.Process.Start(fileReq);
                //System.Diagnostics.Process.Start(fileResp);
                AlterarSchemaXml();

            }


            private XmlDocument MontarXmlConsulta(string[] chaveAcesso)
            {
                var sbXml = new StringBuilder();
                sbXml.Append(@"<?xml version=""1.0"" encoding=""UTF-8""?>
                    <envEvento xmlns=""http://www.portalfiscal.inf.br/nfe"" versao=""1.00"">
                    <idLote>1</idLote>
                    ");
                foreach (var nota in chaveAcesso)
                {
                    sbXml.Append(@"
                    <evento xmlns=""http://www.portalfiscal.inf.br/nfe"" versao=""1.00"">
                    <infEvento Id=""ID210210" + nota + @"01"">
                    <cOrgao>91</cOrgao>
                    <tpAmb>1</tpAmb>
                    <CNPJ>" + cnpj + @"</CNPJ>
                    <chNFe>" + nota + @"</chNFe>
                    <dhEvento>" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-ddTHH:mm:ss") + @"-03:00</dhEvento>
                    <tpEvento>210210</tpEvento>
                    <nSeqEvento>1</nSeqEvento>
                    <verEvento>1.00</verEvento>
                    <detEvento versao=""1.00"">
                    <descEvento>Ciencia da Operacao</descEvento>
                    </detEvento>
                    </infEvento>
                    </evento>
                    ");
                }
                sbXml.Append("</envEvento>");

                var xml = new XmlDocument();
                xml.LoadXml(sbXml.ToString());

                var i = 0;
                foreach (var nota in chaveAcesso)
                {
                    var docXML = new SignedXml(xml);
                    docXML.SigningKey = _nfeRecepcao.ClientCredentials.ClientCertificate.Certificate.PrivateKey;
                    var refer = new Reference();
                    refer.Uri = "#ID210210" + nota + "01";
                    refer.AddTransform(new XmlDsigEnvelopedSignatureTransform());
                    refer.AddTransform(new XmlDsigC14NTransform());
                    docXML.AddReference(refer);
                    var ki = new KeyInfo();
                    ki.AddClause(new KeyInfoX509Data(_nfeRecepcao.ClientCredentials.ClientCertificate.Certificate));
                    docXML.KeyInfo = ki;
                    docXML.ComputeSignature();
                    i++;
                    xml.ChildNodes[1].ChildNodes[i].AppendChild(xml.ImportNode(docXML.GetXml(), true));
                }
                return xml;
            }

            private XmlDocument MontarXmlDownload(string[] chaveAcesso)
            {
                var sbXml = new StringBuilder();
                sbXml.Append(@"<?xml version=""1.0"" encoding=""UTF-8""?>
                <downloadNFe xmlns=""http://www.portalfiscal.inf.br/nfe"" versao=""1.00"">
                <tpAmb>1</tpAmb>
                <xServ>DOWNLOAD NFE</xServ>
                <CNPJ>" + cnpj + @"</CNPJ>
                ");

                foreach (var nota in chaveAcesso)
                {
                    sbXml.Append("<chNFe>" + nota + "</chNFe>");
                }
                sbXml.Append("</downloadNFe>");
                var xml = new XmlDocument();
                xml.LoadXml(sbXml.ToString());
                return xml;
            }

            private string GetSerialCertificado()
            {
                X509Store store = new X509Store("My");
                store.Open(OpenFlags.ReadOnly);
                foreach (var certificado in store.Certificates)
                {
                    if (certificado.SubjectName.Name.Contains(cnpj))
                    {
                        _serialCertificado = certificado.SerialNumber;
                    }
                }
                store.Close();
                return _serialCertificado;
            }
        }

0

Hello,

Recommendation: Perform tests on the Service Type Approval Environment URL Nfedistribuicaodfe

https://hom.nfe.fazenda.gov.br/NFeDistribuicaoDFe/NFeDistribuicaoDFe.asmx

Below is valid xml for desired query:

<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NFeDistribuicaoDFe">
<soap:Body>
    <nfeDistDFeInteresse>
        <nfeDadosMsg>
            <distDFeInt versao="1.00"
                xmlns="http://www.portalfiscal.inf.br/nfe">
                <tpAmb>2</tpAmb>
                <cUFAutor>31</cUFAutor>
                <CNPJ>[cnpj]</CNPJ>
                <distNSU>
                    <ultNSU>000000000000000</ultNSU>
                </distNSU>
            </distDFeInt>
        </nfeDadosMsg>
    </nfeDistDFeInteresse>
</soap:Body>

  • Friend, thanks for the reply, I used exactly your XML (replacing the clear CNPJ), in the approval environment and even then it didn’t work. Until recently this service worked here, but it seems that just started this error.

  • I tested this xml. It’s working. Are you trying some other certificate related error?

  • Do you know the Soapui tool? With Soapui you could configure the certificate and perform connection test with the xml url and tests.

Browser other questions tagged

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