Destruimon Destruimon - 19 days ago 4
Java Question

is there a way for me to pass this soap request as a xml in java?

POST /webservices/producao/cdc/cdc.asmx HTTP/1.1

Host: www.soawebservices.com.br

Content-Type: text/xml; charset=utf-8

Content-Length: length

SOAPAction: "SOAWebServices/PessoaFisicaSimplificada"


<?xml version="1.0" encoding="utf-8"?>

soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"/

soap:Body

<PessoaFisicaSimplificada xmlns="SOAWebServices">
<Credenciais>
<Email>string</Email>
<Senha>string</Senha>
</Credenciais>
<Documento>string</Documento>

<DataNascimento>string</DataNascimento>

</PessoaFisicaSimplificada>


/soap:Body

/soap:Envelope


i have this code i been trying to build:

public void start(){

try {

SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();

String url = "www.soawebservices.com.br";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

printSOAPResponse(soapResponse);

soapConnection.close();
} catch (Exception e) {
System.err.println("Error occurred while sending SOAP Request to Server");
e.printStackTrace();
}

}

private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();

String serverURI = "SOAWebServices/PessoaFisicaSimplificada";



SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("soap", serverURI);


SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("PessoaFisicaSimplificada", "","SOAWebServices");
SOAPElement soapBodyElemC = soapBodyElem.addChildElement("Credenciais");
SOAPElement soapBodyElem1 = soapBodyElemC.addChildElement("Email");
soapBodyElem1.addTextNode("EMAIL");
SOAPElement soapBodyElem2 = soapBodyElemC.addChildElement("Senha");
soapBodyElem2.addTextNode("PASSWORD");
SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("Documento");
soapBodyElem3.addTextNode("CPF");
SOAPElement soapBodyElem4 = soapBodyElem.addChildElement("DataNascimento");
soapBodyElem4.addTextNode("Date");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI);

soapMessage.saveChanges();


System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);
System.out.println();

return soapMessage;
}


however it throws: (javax.xml.soap.SOAPException: JBWS024004: SOAP message could not be sent), i'd guess it's either the uri or the url that i'm passing wrong or i'm not creating the header correctly

the current request it's producing:

SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="SOAWebServices/PessoaFisicaSimplificada"

SOAP-ENV:Header/

SOAP-ENV:Body

<PessoaFisicaSimplificada xmlns="SOAWebServices">
<Credenciais>
<Email>EMAIL</Email>
<Senha>PASSWORD</Senha>
</Credenciais>
<Documento>CPF</Documento>
<DataNascimento>Date</DataNascimento>
</PessoaFisicaSimplificada>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Answer

Ok, it seems you are not providing SOAP Endpoint correctly.

Find below working code. Have just specified full SOAP Endpoint.

public static void main(String[] args) {

    try {

        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        String url = "http://www.soawebservices.com.br/webservices/producao/cdc/cdc.asmx";
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
        soapResponse.writeTo(System.out);
        //printSOAPResponse(soapResponse);

        soapConnection.close();
    } catch (Exception e) {
        System.err.println("Error occurred while sending SOAP Request to Server");
        e.printStackTrace();
    }

}

private static SOAPMessage createSOAPRequest() throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    String serverURI = "SOAWebServices/PessoaFisicaSimplificada";



    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration("soap", serverURI);


    SOAPBody soapBody = envelope.getBody();
    SOAPElement soapBodyElem = soapBody.addChildElement("PessoaFisicaSimplificada", "","SOAWebServices");
    SOAPElement soapBodyElemC = soapBodyElem.addChildElement("Credenciais");
    SOAPElement soapBodyElem1 = soapBodyElemC.addChildElement("Email");
    soapBodyElem1.addTextNode("EMAIL");
    SOAPElement soapBodyElem2 = soapBodyElemC.addChildElement("Senha");
    soapBodyElem2.addTextNode("PASSWORD");
    SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("Documento");
    soapBodyElem3.addTextNode("CPF");
    SOAPElement soapBodyElem4 = soapBodyElem.addChildElement("DataNascimento");
    soapBodyElem4.addTextNode("Date");
    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction", serverURI);

    soapMessage.saveChanges();


    System.out.print("Request SOAP Message = ");
    soapMessage.writeTo(System.out);
    System.out.println();

    return soapMessage;
}
Comments