Enviar CC-e - Ajuda

0 respostas
A

Bom dia pessoal,

Não sei se estou postando minha dúvida no fórum correto, mas não encontrei nenhum que tratasse especificamente de NF-e ou CC-e então estarei postando neste, caso esteja errado, por favor me avisem.

Estou tendo problemas para enviar uma carta de correção eletrônica de meu sistema…

Segue o código que estou usando:

public void enviarCartaCorrecaoReceita() {
		
		try {
			
			BufferedReader br = new BufferedReader(new FileReader(arquivoXml));
			String aux;
			String dados = "";
			while ((aux = br.readLine()) != null) {
				dados += aux;
			}
			br.close();
			
			dados = dados.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
			
			System.setProperty("javax.net.ssl.trustStoreType", "JKS");  
	        System.setProperty("javax.net.ssl.trustStore", "jssecacerts");  
	        System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
			
			if(!Var.isA3){
				
				System.setProperty("javax.net.ssl.keyStoreType", "PKCS12");  
				System.setProperty("javax.net.ssl.keyStore", certificado.getAbsolutePath());  
				System.setProperty("javax.net.ssl.keyStorePassword", senha);  
				   
			}else{
				Provider p = new SunPKCS11("token.cfg");
				Security.addProvider(p);
				KeyStore ks = KeyStore.getInstance("PKCS11");
				ks.load(null, senha.toCharArray());
				
				System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");  
	            System.setProperty("javax.net.ssl.keyStoreType",ks.getType());
	            System.setProperty("javax.net.ssl.keyStore", "NONE"); 
	            System.setProperty("javax.net.ssl.keyStorePassword", senha);
			}
			
			StringBuffer soapMessage = new StringBuffer();
			soapMessage.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
					   .append("<soap12:Envelope 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("<soap12:Header>")
					   .append("<nfeCabecMsg xmlns=\"http://www.portalfiscal.inf.br/nfe/wsdl/NFeRecepcaoEvento\">")  
					   .append("<versaoDados>2.00</versaoDados>")
					   .append("<cUF>41</cUF>")
					   .append("</nfeCabecMsg>")
					   .append("</soap12:Header>")
					   .append("<soap12:Body>")
					   .append(dados)
					   .append("</soap12:Body>")
					   .append("</soap12:Envelope>");
			
			String response = metodo2(soapMessage.toString(), "https://nfe2.fazenda.pr.gov.br/nfe-evento/NFeRecepcaoEvento");
			//"https://nfe2.fazenda.pr.gov.br/nfe-evento/NFeRecepcaoEvento"
															  
			//"https://nfe2.fazenda.pr.gov.br/nfe/NFeRecepcao2"
			//"https://nfe2.fazenda.pr.gov.br/nfe-evento/NFeRecepcaoEvento?wsdl"
			
			try{
				
				File retorno = new File(arquivoXml.getAbsolutePath().replace(".xml", "-retEnvCCe.xml"));
				BufferedWriter bw = new BufferedWriter(new FileWriter(retorno));
				bw.write(response);
				
				bw.flush();
				bw.close();
				
			}catch(Exception e){
				JOptionPane.showMessageDialog(null, e.getMessage());
				e.printStackTrace();
			}
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return;
		} catch (IOException e) {
			e.printStackTrace();
			return;
		} catch (KeyStoreException e) {
			e.printStackTrace();
			return;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return;
		} catch (CertificateException e) {
			e.printStackTrace();
			return;
		}
		
	}
	
	public String metodo2(String envelope, String urlAddress) {  

		try {  
			MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);  
			SOAPMessage message;  

			try {  
				MimeHeaders header = new MimeHeaders();  
				header.addHeader("Content-Type", "application/soap+xml");  

				message = factory.createMessage(header, new ByteArrayInputStream(envelope.getBytes()));  
				SOAPConnection con = SOAPConnectionFactory.newInstance().createConnection();  

				URL url = new URL(urlAddress);  
				System.out.println("Message enviada \n"+message);  
				SOAPMessage res = con.call(message, url);  

				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()));  
				return desnormalizar(out.toString());
				
			} catch (IOException ex) {
				System.out.println("" + ex.getMessage());
				return null;
			}
		} catch (SOAPException ex) {
			System.out.println("" + ex.getMessage());
			return null;
		}
	}

	public String desnormalizar(String texto) {  
		return texto.replace("<", "<").replace(">", ">").replace("''", "\"").replace("\n", "\r");  
	}

e ai o eclipse retorna o seguinte erro ao tentar enviar o arquivo:

21/09/2012 09:04:06 com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection post
GRAVE: SAAJ0009: Message send failed
java.security.PrivilegedActionException: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Message send failed

esta é a linha na qual tento enviar o arquivo:

SOAPMessage res = con.call(message, url);

Alguém tem alguma idéia do que pode estar acontecendo e como posso resolver isso?

Grato

Criado 21 de setembro de 2012
Respostas 0
Participantes 1