Consumir Web Service Nota Control

0 respostas
J

Boa Tarde pessoal, estou tentando desenvolver um programa para Assinar e Enviar o XML da NFS-e da Prefeitura de Varzea Grande/MT, a parte da assinatura já está ok,
porém não consegui fazer a comunicação com o WebService, toda vez que tento fazer a comunicação recebo a seguinte mensagem : SEVERE: SAAJ0008: Bad Response; Bad Request

estou colocando abaixo o código da classe que uso para enviar o XML para o WebService, es alguém puder me ajudar, agradeço desde já.

import br.com.ccotecnologia.nfse.javabean.Configuracoes;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import java.io.FileOutputStream;
import java.io.OutputStream;

import java.net.URL;

import java.security.Security;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPMessage;

public class EnvNFSE {
	public String enviaNfse(String enderecoWS, String xml,
                          Configuracoes config)
	{
          System.out.println("EnderecoWS = " + enderecoWS);
          String retorno = "";
                  
          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("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", config.getCertificadoDigital());  
          System.setProperty("javax.net.ssl.keyStorePassword", config.getSenhaCertificado());
		
            try
            {
                    //Object[] parametros = {new String(xml)};
            
                    //Service servico = new Service();
                    //Call chamada = (Call) servico.createCall();
                    //chamada.setTargetEndpointAddress(enderecoWS);
                    //chamada.setOperationName("RecepcionarLoteRps");
                    //retorno = (String) chamada.invoke(parametros);
            
                    CriptografaSha1 chiptografar = new CriptografaSha1();
                    StringBuilder builder = new StringBuilder(xml);
                    builder.delete(0, 38);	
                    
                    xml = builder.toString();
                                            
                    StringBuffer soapMessage = new StringBuffer();
                    StringBuffer soapBody = new StringBuffer();
                    //xmlns="http://schemas.microsoft.com/2003/10/Serialization/"
                    
                    soapBody.append("<soap12:Body> ")
                            .append("<NfdEntrada xmlns=\"http://www.issnetonline.com.br/webservice/nfd\"> ")
                            .append("<CpfUsuario>[telefone removido]</CpfUsuario> ")
                            .append("<HashSenha>" + chiptografar.criptografa("1234") + "</HashSenha> ")
                            .append("<CodigoMunicipio>999</CodigoMunicipio>")
                            .append("<Nfd>" + xml +"</Nfd>")
                            .append("</NfdEntrada>")
                            .append("</soap12:Body>");                     
                      
                    soapMessage.append("<?xml version=\"1.0\" encoding=\"utf-8\"?> ")
                                .append("<soap12:Envelope ")
                                .append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ")
                                .append("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ")
                                .append("xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">")
                                .append(soapBody)
                                .append("</soap12:Envelope> ");
                    
                    MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
                    SOAPMessage message;
                    
                    try 
                    {
                            Integer tamanhoBody = soapBody.length();
                            MimeHeaders header = new MimeHeaders();
                            header.addHeader("Content-Type", "application/soap+xml");
                            header.addHeader("Content-Length", tamanhoBody.toString());
                                                            
                            message = factory.createMessage(header, new ByteArrayInputStream(soapMessage.toString().getBytes()));
                            
                            SOAPConnection conexao = SOAPConnectionFactory.newInstance().createConnection();
                            URL urlWebService = new URL(enderecoWS);
                            
                            OutputStream output = new FileOutputStream("c:\\NFSE\\SOAP.TXT");
                            message.writeTo(output);
                            output.close();
                            
                            SOAPMessage res = conexao.call(message, urlWebService);
            
                            ByteArrayOutputStream in = new ByteArrayOutputStream();
                            message.writeTo(in);
                            System.out.println("in :\n" + in.toString());
                            
                            ByteArrayOutputStream out = new ByteArrayOutputStream();
                            res.writeTo(out);
                            System.out.println("out :\n" + desnormalizar(out.toString()));
                            
            } 
                    catch (Exception e) 
                    {
                            e.printStackTrace();
                            //System.exit(1);
                    }
                    
            }
            catch (Exception e)
            {
                    e.printStackTrace();
                    //System.exit(1);
            }
            finally
            {
                    return retorno;
            }
		
	}
	
	public static String desnormalizar(String texto) 
	{
		return texto.replace("<", "<").replace(">", ">").replace("\"", "\"").replace("		", "\r");
	}
}

Estou colocando em Anexo uma mensagem SOAP que estou criando.

Criado 17 de setembro de 2012
Respostas 0
Participantes 1