Versão atual:

Erro com o envio SOAP da nfe.

Amigos boa tarde,

Estou tentando consumar um WebService com a fazenda, no serviço mais simples que é consultar o status. Consigo obter uma resposta do servidor, (Cod 500) ou seja não entendeu o que eu enviei.

Verificando o que o SOAP enviou, está correto o cabeçalho mas o Body está com problemas, pois a onde deveria estar < > aparece &lt &gl. Creio que esse seja o problema total.

Segue meu código:

package nfe_teste;

import br.inf.portalfiscal.nfe.*;
import br.inf.portalfiscal.nfe.wsdl.nfestatusservico2.*;
import java.io.IOException;
import java.io.StringWriter;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.cert.CertificateException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.stream.XMLStreamException;
import javax.xml.ws.Holder;

/**
 *
 * @author Marcelo
 */
public class NFe_Teste {

    /**
     * @param args the command line arguments
     * @throws java.security.KeyStoreException
     * @throws java.security.NoSuchAlgorithmException
     * @throws java.security.cert.CertificateException
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws KeyStoreException, NoSuchAlgorithmException, IOException, CertificateException, JAXBException, XMLStreamException {

        System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");
        System.setProperty("com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.dump", "true");

        TConsStatServ consStatServ = new br.inf.portalfiscal.nfe.ObjectFactory().createTConsStatServ();
        consStatServ.setCUF("35");
        consStatServ.setTpAmb("2");
        consStatServ.setVersao("2.00");
        consStatServ.setXServ("STATUS");

        JAXBContext context = JAXBContext.newInstance("br.inf.portalfiscal.nfe");

        Marshaller marshaller = context.createMarshaller();
        JAXBElement<TConsStatServ> JXconsStatServ = new br.inf.portalfiscal.nfe.ObjectFactory().createConsStatServ(consStatServ);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);


        StringWriter resultXML = new StringWriter();        
        marshaller.marshal(JXconsStatServ, resultXML);

        NfeDadosMsg nfeDadosMsg = new br.inf.portalfiscal.nfe.wsdl.nfestatusservico2.ObjectFactory().createNfeDadosMsg();
        nfeDadosMsg.getContent().add(resultXML.toString());

        NfeCabecMsg nfeCabecMsg = new br.inf.portalfiscal.nfe.wsdl.nfestatusservico2.ObjectFactory().createNfeCabecMsg();

        nfeCabecMsg.setCUF("35");
        nfeCabecMsg.setVersaoDados("2.00");

        Holder<NfeCabecMsg> JXnfeCabecMsg = new Holder<>(nfeCabecMsg);

            System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");  
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());  
            System.setProperty("sun.security.ssl.allowUnsafeRenegotiation ", "true");

            System.setProperty("javax.net.ssl.keyStoreType", "PKCS12");  

            System.clearProperty("javax.net.ssl.keyStore");  
            System.clearProperty("javax.net.ssl.keyStorePassword");  
            System.clearProperty("javax.net.ssl.trustStore");  

            System.setProperty("javax.net.ssl.keyStore", "C:\\cert\\certificadocomSenha.pfx");  
            System.setProperty("javax.net.ssl.keyStorePassword", "1234");  

            System.setProperty("javax.net.ssl.trustStoreType", "JKS");  
            System.setProperty("javax.net.ssl.trustStore", "C:\\cert\\NFeCacerts");
            System.setProperty("javax.net.ssl.trueStorePassword", "changeit");


        NfeStatusServicoNF2Result result;
        NfeStatusServico2Soap12 consulta = new br.inf.portalfiscal.nfe.wsdl.nfestatusservico2.NfeStatusServico2().getNfeStatusServico2Soap12();
        try {
             result = consulta.nfeStatusServicoNF2(nfeDadosMsg, JXnfeCabecMsg);
             System.out.println(result.getContent());
        } catch (javax.xml.ws.WebServiceException ex) {
            // TODO Handle exception
           java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
        }
    }
}

e segue o retorno.

run:
---[HTTP request - https://nfe.fazenda.sp.gov.br/nfeweb/services/nfestatusservico2.asmx]---
Accept: [application/soap+xml, multipart/related]
Content-Type: [application/soap+xml; charset=utf-8;action="http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico2/nfeStatusServicoNF2"]
User-Agent: [JAX-WS RI 2.2.4-b01]
<?xml version="1.0" ?><S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope"><S:Header><nfeCabecMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico2"><cUF>35</cUF><versaoDados>2.00</versaoDados></nfeCabecMsg></S:Header><S:Body><nfeDadosMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NfeStatusServico2">&lt;consStatServ versao=&quot;2.00&quot; xmlns=&quot;http://www.portalfiscal.inf.br/nfe&quot;&gt;&lt;tpAmb&gt;2&lt;/tpAmb&gt;&lt;cUF&gt;35&lt;/cUF&gt;&lt;xServ&gt;STATUS&lt;/xServ&gt;&lt;/consStatServ&gt;</nfeDadosMsg></S:Body></S:Envelope>--------------------
---[HTTP response - https://nfe.fazenda.sp.gov.br/nfeweb/services/nfestatusservico2.asmx - 500]---
null: [HTTP/1.1 500 Internal Server Error]
Cache-Control: [private]
Content-Length: [618]
Content-Type: [application/soap+xml; charset=utf-8]
Date: [Thu, 01 May 2014 15:57:45 GMT]
Server: [Microsoft-IIS/6.0]
X-AspNet-Version: [4.0.30319]
X-Powered-By: [ASP.NET]
<?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><soap:Fault><soap:Code><soap:Value>soap:Sender</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">Server was unable to read request. ---&gt; There is an error in XML document (1, 550). ---&gt; The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type.</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>--------------------
Mai 01, 2014 12:57:45 PM nfe_teste.NFe_Teste main
Grave: null
javax.xml.ws.soap.SOAPFaultException: Server was unable to read request. ---> There is an error in XML document (1, 550). ---> The specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type.
    at com.sun.xml.internal.ws.fault.SOAP12Fault.getProtocolException(SOAP12Fault.java:214)
    at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:117)
    at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:108)
    at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78)
    at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:135)
    at com.sun.proxy.$Proxy33.nfeStatusServicoNF2(Unknown Source)
    at nfe_teste.NFe_Teste.main(NFe_Teste.java:91)

BUILD SUCCESSFUL (total time: 2 seconds)

Já não sei mais o que fazer para que o SOAP leia de forma correta a String.

Versão(1):

Ver a versão formatada

Erro com o envio SOAP da nfe.

Comentário

new question