使用SOAP WebService与Java问题

时间:2015-10-28 11:30:21

标签: java web-services soap wsdl

我在stackoverflow上阅读了很多教程和类似的问题,但是我仍然在连接到我的SOAP服务时遇到问题。

我正在尝试使用Java调用SOAP Web服务。我在这里找到了一个很好的答案:https://stackoverflow.com/a/15942217/2145530

该示例完全正常,但当我将其更改为另一个wsdl文件时,它不再有效:

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

    // Send SOAP Message to SOAP Server
    String url = "http://192.168.200.165/soap/server.php";
    SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

    // print SOAP Response
    System.out.print("Response SOAP Message:");
    soapResponse.writeTo(System.out);

    soapConnection.close();
}

private static SOAPMessage createSOAPRequest() throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    String serverURI = "urn:BoardSOAP";

    // SOAP Envelope
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration("soap", serverURI);

    System.out.println(envelope.getNamespaceURI());

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

    soapMessage.saveChanges();

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

    return soapMessage;
}

如你所见,我改变了这些界限:

String url = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";
String serverURI = "http://ws.cdyne.com/";
headers.addHeader("SOAPAction", serverURI  + "ReturnCodes");

String url = "http://192.168.200.165/soap/server.php";
String serverURI = "urn:BoardSOAP";
headers.addHeader("SOAPAction", serverURI  + "getBoardStatus");

这是我的wsdl文件:wsdl

完整输出:

    http://schemas.xmlsoap.org/soap/envelope/
Request SOAP Message:<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="urn:BoardSOAP"><SOAP-ENV:Header/><SOAP-ENV:Body/></SOAP-ENV:Envelope>
Response SOAP Message:<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body><SOAP-ENV:Fault><faultcode xsi:type="xsd:string">SOAP-ENV:Client</faultcode><faultactor xsi:type="xsd:string"></faultactor><faultstring xsi:type="xsd:string">Operation &apos;&apos; is not defined in the WSDL for this service</faultstring><detail xsi:type="xsd:string"></detail></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>

WSDL文件没有损坏,我可以通过SoupUI与它进行通信,没有任何问题。

1 个答案:

答案 0 :(得分:2)

在您引用的答案中,soapAction的{​​{1}}为ReturnCodes,由http://ws.cdyne.com/ReturnCodes

行构成

但是,在您的WSDL中,由于您不使用URL而是使用URN作为命名空间标识符,headers.addHeader("SOAPAction", serverURI + "ReturnCodes");的{​​{1}}为soapAction。如果您使用相同的连接方案,那么您将错过使用getBoardStatus而不是urn:server#getBoardStatus的{​​{1}}

尝试使用`headers.Header(&#34; SOAPAction&#34;,&#34; urn:server#getBoardStatus&#34;)。