Java 中的 SOAP 网络服务客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19605445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:43:22  来源:igfitidea点击:

SOAP Webservice Client in Java

javaweb-servicessoapwebservice-client

提问by Dharam Chag

I am newbie in SOAP webservice client and getting errors while creating client.

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

please help me to solve this

请帮我解决这个问题

//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 Code

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

Please help me to create client.

请帮我创建客户端。

采纳答案by constantlearner

I will suggest debugging in 2 steps

我建议分两步调试

1)Use soapUIand check whether your response in coming or not

1)使用soapUI并检查您的响应是否到来

2)Use the working example i used from mykong

2)使用我从mykong使用的工作示例