Duvida com o webservice nfeconsultanfe

0 respostas
jonasjgs2

alguem ja trabalhou com isso?

eu consigo me comunicar com ele porem ele nao me traz todas as notas se alguem poder me ajudar eu agradeco
segue o meu codigo

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Nfe200;

import br.inf.portalfiscal.nfe.TRetConsNFeDest;
import br.inf.portalfiscal.www.nfe.wsdl.nfeconsultadest.NFeConsultaDestStub;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLStreamException;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.commons.httpclient.protocol.Protocol;

/**
 *
 * @author Jonas
 */
public class NFeConsultaNFeDest {

    public static void main(String[] args) {

        String CNPJ = "09564954000100";
        String CodEstado = "35";
        String TipoAmbiente = "1";
        String Url = "https://www.nfe.fazenda.gov.br/NFeConsultaDest/NFeConsultaDest.asmx";
        String CertHexa = "754d820aa0767462133ca047e448b4b6";
        String CertSenha = "123456";
        String ArqCacerts = "c:/EsquemasXml/Certificados/NfeCacerts.jks";
        String VersaoXml = "1.01";
        String UltNSU = "0";
        NFeConsultaNFeDest consnfe = new NFeConsultaNFeDest();
        TRetConsNFeDest rcnfe = null;
        String indcont = "1";


        while (indcont.equals("1")) {
            try {
                rcnfe = consnfe.NFeConsultaNFe(CNPJ, CodEstado, TipoAmbiente, Url, CertHexa, CertSenha, ArqCacerts, VersaoXml, UltNSU);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            UltNSU = rcnfe.getUltNSU();
            UltNSU = "0";                                            ja coloquei esta linha aqui para ve se trazia todas as notas mas nao traz...
            indcont = rcnfe.getIndCont();
            System.out.println("Indicador de continuidade " + indcont);
        }
    }

    public TRetConsNFeDest NFeConsultaNFe(String CNPJ, String CodEstado, String TipoAmbiente, String Url, String CertHexa, String CertSenha, String ArqCacerts, String VersaoXml, String UltNSU)
            throws MalformedURLException, UnsupportedEncodingException, FileNotFoundException, IOException, XMLStreamException, JAXBException {
        TRetConsNFeDest rcnfe = null;
        URL url = new URL(Url);
        NFeCertificado n = new NFeCertificado();
        X509Certificate certificate = n.PegaCertificado(CertHexa);
        PrivateKey privateKey = n.PegaChavePrivada(certificate, CertSenha);
        SocketFactoryDinamico socketFactoryDinamico = new SocketFactoryDinamico(certificate, privateKey);
        socketFactoryDinamico.setFileCacerts(ArqCacerts);

        int SSL_PORT = 443;

        Protocol protocol = new Protocol("https", socketFactoryDinamico, SSL_PORT);
        Protocol.registerProtocol("https", protocol);

        StringBuilder xml = new StringBuilder();
        xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
                .append("<consNFeDest versao=\"")
                .append(VersaoXml)
                .append("\" xmlns=\"http://www.portalfiscal.inf.br/nfe\">")
                .append("<tpAmb>")
                .append(TipoAmbiente)
                .append("</tpAmb>")
                .append("<xServ>CONSULTAR NFE DEST</xServ>")
                .append("<CNPJ>")
                .append(CNPJ)
                .append("</CNPJ>")
                .append("<indNFe>")
                .append("0")
                .append("</indNFe>")
                .append("<indEmi>")
                .append("0")
                .append("</indEmi>")
                .append("<ultNSU>")
                .append(UltNSU)
                .append("</ultNSU>")
                .append("</consNFeDest>");


        OMElement ome = AXIOMUtil.stringToOM(xml.toString());

        NFeConsultaDestStub.NfeDadosMsg dadosMsg = new NFeConsultaDestStub.NfeDadosMsg();
        dadosMsg.setExtraElement(ome);
        NFeConsultaDestStub.NfeCabecMsg nfeCabecMsg = new NFeConsultaDestStub.NfeCabecMsg();
        /**
         * Código do Estado.
         */
        nfeCabecMsg.setCUF(CodEstado);

        /**
         * Versao do XML
         */
        nfeCabecMsg.setVersaoDados(VersaoXml);

        NFeConsultaDestStub.NfeCabecMsgE nfeCabecMsgE = new NFeConsultaDestStub.NfeCabecMsgE();
        nfeCabecMsgE.setNfeCabecMsg(nfeCabecMsg);

        NFeConsultaDestStub stub = new NFeConsultaDestStub(url.toString());
        NFeConsultaDestStub.NfeConsultaNFDestResult result = stub.nfeConsultaNFDest(dadosMsg, nfeCabecMsgE);

        String s = result.getExtraElement().toString();

        JAXBContext context = JAXBContext.newInstance(TRetConsNFeDest.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        rcnfe = (TRetConsNFeDest) unmarshaller.unmarshal(new ByteArrayInputStream(s.getBytes("UTF-8")));

        int i = 0;
        System.out.println("-> NSU " + UltNSU + "---------------------------------------------");
        for (i = 0; i < rcnfe.getRet().size(); i++) {
            System.out.println("i=" + i + "---------------------------------------------------");
            System.out.println("-> NSU " + UltNSU + "- "
                    + rcnfe.getRet().get(i).getResNFe().getCNPJ() + "-"
                    + rcnfe.getRet().get(i).getResNFe().getXNome() + "-"
                    + rcnfe.getRet().get(i).getResNFe().getChNFe() + "-"
                    + rcnfe.getRet().get(i).getResNFe().getVNF() + "-");
        }

        return rcnfe;
    }
}
Criado 10 de dezembro de 2012
Respostas 0
Participantes 1