有没有办法让我在java中将这个soap请求作为xml传递?

时间:2016-11-15 11:41:50

标签: java soap

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

我有这个代码我一直在努力建立:

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;
}

然而它抛出:(javax.xml.soap.SOAPException:JBWS024004:无法发送SOAP消息),我猜它是uri或url我传递错误或者我没有创建标题正确

它正在产生的当前请求:

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>

1 个答案:

答案 0 :(得分:0)

好的,您似乎没有正确提供SOAP端点。

查找以下工作代码。刚刚指定了完整的SOAP端点。

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;
}