java - 如何用java编写soap客户端?

标签 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代码?

感谢您的帮助!

最佳答案

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

尝试

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

而不是

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

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

关于java - 如何用java编写soap客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29272841/

相关文章:

xml - clojure 命名空间感知 xml 解析器/zipper

java - 为第三方创建/公开 Web 服务的设计决策

android - 使用 Ksoap2 for android 更改 SOAP 信封的布局

php - 如何使用 SoapClient 类进行 PHP SOAP 调用

java - 从 App Engine 应用程序向 Google 云打印提交打印作业

java - 如何开发支持多个API的android应用

java - onBackPressed() 最佳实践/性能

java - 测试运行半个多小时

java - 哨兵值(value)实现

PHP 5 SoapServer 用法?