如何在java中编写soap客户端代码?

时间:2015-03-26 07:12:19

标签: java soap

我有和xml

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <soap:Body>
<SendRequest xmlns="http://tempuri.org/">
  <auth xmlns="">
    <Login xmlns="http://tempuri.org/">vcm</Login>
    <Password xmlns="http://tempuri.org/">vcm</Password>
  </auth>
  <Request xmlns="">
        <Request_code xmlns="http://     tempuri.org/">1</Request_code>
        <Message_Code xmlns="http://tempuri.org/">1111</Message_Code>
        <Params xmlns="http://  tempuri.org/">
      <RequestParameter>
        <Name />
        <Value />
      </RequestParameter>
    </Params>
  </Request>
   </SendRequest>
 </soap:Body>
</soap:Envelope>

和Java代码

MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    // SOAP Envelope
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.removeAttribute("xmlns:SOAP-ENV");
    envelope.setPrefix("soap");
    envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
    envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");

    MimeHeaders mimeheaders = soapMessage.getMimeHeaders();
    mimeheaders.addHeader("SOAPAction", "SendRequest");
    SOAPHeader header = soapMessage.getSOAPHeader();
    header.detachNode();

    SOAPBody soapBody = envelope.getBody();
    soapBody.setPrefix("soap");
    SOAPElement sendRequest = soapBody.addChildElement("SendRequest");
    SOAPElement auth = sendRequest.addChildElement("auth");
    auth.addChildElement("Login")
        .addTextNode("vcm");
    auth.addChildElement("Password")
        .addTextNode("vcm");

    SOAPElement request = sendRequest.addChildElement("Request");
    request.addChildElement("Request_code")
            .addTextNode("1");
    request.addChildElement("Message_Code")
            .addTextNode("1111");
    request.addChildElement("Params");
    sendRequest.addAttribute(new QName("xmlns"), "http://tempuri.org/");
    soapMessage.saveChanges();

    /* Print the request message */
    System.out.print("Request SOAP Message:");
    soapMessage.writeTo(System.out);
    System.out.println();

我控制台我看到没有“http://tempuri.org/”的标签。 如果我正在写作,例如,

sendRequest.addAttribute(new QName("aaa"), "http://tempuri.org/") 

我有

<SendRequest aaa="http://tempuri.org/">

如何编写java代码?

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您没有将命名空间添加到元素中。

尝试

SOAPElement sendRequest = soapBody.addChildElement(
  new QName("http://tempuri.org/", "SendRequest"));

而不是

SOAPElement sendRequest = soapBody.addChildElement("SendRequest");

请注意,将命名空间URI的多个引用分解为常量/变量可能是个好主意。

答案 1 :(得分:0)

感谢您的帮助! 这段代码工作正常

  MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    // SOAP Envelope
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.removeAttribute("xmlns:SOAP-ENV");
    envelope.setPrefix("soap");
    envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
    envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");

    MimeHeaders mimeheaders = soapMessage.getMimeHeaders();
    mimeheaders.addHeader("SOAPAction", "SendRequest");
    SOAPHeader header = soapMessage.getSOAPHeader();
    header.detachNode();

    SOAPBody soapBody = envelope.getBody();
    soapBody.setPrefix("soap");
    QName xmlns = new QName("xmlns");
    SOAPElement sendRequest = soapBody.addChildElement(new QName("http://tempuri.org/", "SendRequest"));
    sendRequest.addAttribute(xmlns, "");
    SOAPElement auth = sendRequest.addChildElement(new QName("", "auth"));

    auth.addChildElement(new QName("http://tempuri.org/", "Login"))
        .addAttribute(new QName("xmlns"), "http://tempuri.org/")
        .addTextNode("vcm");
    auth.addChildElement(new QName("http://tempuri.org/", "Password"))
        .addAttribute(new QName("xmlns"), "http://tempuri.org/")
        .addTextNode("vcm");

    SOAPElement request = sendRequest.addChildElement(new QName("", "Request"));
    request.addAttribute(new QName("xmlns"), "");

    request.addChildElement(new QName("http://tempuri.org/", "Request_code"))
            .addAttribute(new QName("xmlns"), "http://tempuri.org/")
            .addTextNode("1");
    request.addChildElement(new QName("http://tempuri.org/", "Message_Code"))
            .addAttribute(new QName("xmlns"), "http://tempuri.org/")
            .addTextNode("1111");

    SOAPElement params = request.addChildElement(new QName("http://tempuri.org/", "Params"));

    SOAPElement requesParameter = params.addChildElement("RequestParameter");
    requesParameter.addChildElement("Name");
    requesParameter.addChildElement("Value");

    soapMessage.saveChanges();

    /* Print the request message */
    System.out.print("Request SOAP Message:");
    soapMessage.writeTo(System.out);
    System.out.println();