java - Java 中的 SOAP Web 服务客户端

标签 java web-services soap webservice-client

我是 SOAP Web 服务客户端的新手,在创建客户端时遇到错误。

请帮我解决这个问题

//This is request that has to be send using SOAP Envelope

POST /DISWebService/DISWebService.asmx HTTP/1.1
Host: 192.168.2.119
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <LoginSystem xmlns="http://tempuri.org/">
      <username>string</username>
      <password>string</password>
    </LoginSystem>
  </soap12:Body>
</soap12:Envelope>

Java 代码

public static void main(String args[]) {
    try {
        // Create SOAP Connection
    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory      .newInstance();
    SOAPConnection soapConnection = soapConnectionFactory
                    .createConnection();

            String url = "http://192.168.2.119/VISWebService/VISWebService.asmx";
            // String url =
            // "http://192.168.2.119/DISWebService/DISWebService.asmx?op=LoginSystem";

            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(),url);

            // Process the SOAP Response
            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 = "http://192.168.2.119/DISWebService/DISWebService.asmx";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();

        // SOAP Body
        SOAPBody soapBody = envelope.getBody();

        SOAPElement soapBodyElem = soapBody.addChildElement("LoginSystem");

        SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("username");
        soapBodyElem1.addTextNode("Chirendu");

        SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("password");
        soapBodyElem2.addTextNode("verve12*");

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", serverURI );

        soapMessage.saveChanges();

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

        return soapMessage;
    }

请帮我创建客户端。

最佳答案

我建议分两步进行调试

1)使用soapUI并检查您的回复是否到来

2)使用我在 mykong 中使用的工作示例

关于java - Java 中的 SOAP Web 服务客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19605445/

相关文章:

java - '没有合适的驱动程序'错误Android mySql

java - 如何创建向上计数的字母数字标识符?

java - 如何将一个数字的最后一位数字与另一个数字的最后一位数字进行比较?

xml - WP7 从 xml Web 服务获取和存储数据以供整个项目使用

java - 用于针对用户输入进行快速查找服务的 REST 或 SOAP?

java - 静态类中的 IO 异常 - java

java - 如何在 Java 中使用 RESTful Web 服务获取远程/客户端 IP 地址?

web-services - 确保 REST API 安全,无需重新发明轮子

soap - 在 Powershell 中使用 Soap complexType 来保持 Soap 服务的热度

ios - 如何使用netsuite API在移动设备上获取数据?