Oi pessoal, estou a alguns dias tentando obter o retorno da API em XML quando ocorrer um ERRO, porém nada é retornado, quando a requisição dá certo funciona normalmente. Meu código é este:
public static void main ( String args []) throws Exception
{
operacaoSerasa();
}
public static void operacaoSerasa() throws Exception {
URL url = new URL("https://treina.spc.org.br/spc/remoting/ws/insumo/spc/spcWebService?wsdl\"");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
String requestBodyXML = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.spc.insumo.spcjava.spcbrasil.org/\"> <soapenv:Header/> <soapenv:Body> <web:incluirSpc> <insumoSpc> <tipo-pessoa>J</tipo-pessoa> <dados-pessoa-juridica> <cnpj numero=\"30585461000134\"/> <razao-social> IMPORTADORA MACAPA RURAL CASA E CONSTRUCAO LTDA </razao-social> <nome-comercial>IMPORTADORA MACAPA, RURAL CASA E CONSTRUCAO</nome-comercial> </dados-pessoa-juridica> <data-compra>2022-07-29T00:00:00</data-compra> <data-vencimento>2022-11-17T00:00:00</data-vencimento> <codigo-tipo-devedor>C</codigo-tipo-devedor> <numero-contrato>30585461000134C947272</numero-contrato> <valor-debito> 1436.08</valor-debito> <natureza-inclusao> <id>1</id> </natureza-inclusao> <endereco-pessoa> <cep>-68905627</cep> <logradouro>JOSE DO ESPIRITO SANTO ARAUJO</logradouro> <bairro>PERPETUO SOCORRO</bairro> <numero>283</numero> </endereco-pessoa> </insumoSpc> </web:incluirSpc> </soapenv:Body> </soapenv:Envelope>";
StringBuffer response = new StringBuffer();
String inputLine = "";
//connection.setRequestProperty("Content-Type", "application/xml");
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", "Basic MTEyMDIy");
connection.setRequestProperty("Content-Length", "<calculated when request is sent>");
connection.setRequestProperty("Host", "<calculated when request is sent>");
connection.setRequestProperty("User-Agent", "PostmanRuntime/7.29.2");
connection.setRequestProperty("Accept", "application/xml?");
connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestProperty("Content-Type", "text/xml charset=utf-8");
connection.setDoOutput(true);
connection.setReadTimeout(30000);
connection.setConnectTimeout(30000);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(requestBodyXML);
wr.flush();
wr.close();
try {
int responseCode = connection.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
if (responseCode == HttpURLConnection.HTTP_OK &&
responseCode == HttpURLConnection.HTTP_CREATED ||
responseCode == HttpURLConnection.HTTP_ACCEPTED) {
throw new Exception("Erro de conexão, veja mais informações: ");
}
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
} catch (Exception erro) {
throw new Exception("<br>Cód. Resposta Servidor: " + connection.getResponseCode() + "<br/>"
+ erro.toString() + "<br/>" + response.toString()
+ connection.getErrorStream() );
} finally {
connection.disconnect();
}
}
O erro que eu recebo não é um XML dentro do Eclipse.
O erro é proposital, retorno da API no Postman:
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
<faultcode>CS_AUT001.E1.2</faultcode>
<faultstring>
<![CDATA[Operador ou senha inválidos. Tente novamente.]]>
</faultstring>
</S:Fault>
</S:Body>
</S:Envelope>