简体   繁体   English

有没有办法让我将此肥皂请求作为java中的xml传递?

[英]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 但是它抛出:(javax.xml.soap.SOAPException:JBWS024004:无法发送SOAP消息),我猜这是我传递错误的uri或url或我没有正确创建标头

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>

Ok, it seems you are not providing SOAP Endpoint correctly. 好的,看来您没有正确提供SOAP端点。

Find below working code. 查找以下工作代码。 Have just specified full SOAP Endpoint. 刚刚指定了完整的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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM