Web Services 使用SOAP Web服务时出现Java问题

qaxu7uf2  于 2022-11-15  发布在  Java
关注(0)|答案(2)|浏览(238)

我阅读了很多关于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文件:

全输出:

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与它通信,没有问题。

vcudknz3

vcudknz31#

在你给出的答案中,ReturnCodessoapActionhttp://ws.cdyne.com/ReturnCodes,它是由headers.addHeader("SOAPAction", serverURI + "ReturnCodes");线构造的
但是,在WSDL中,由于您使用的名称空间标识符不是URL而是URN,因此getBoardStatussoapActionurn:server#getBoardStatus。如果使用相同的串联方案,则将缺少#**,**您将使用urn:BoardSoap而不是urn:server
请尝试使用'头文件。头文件(“SOAPAction”,“urn:server#getBoardStatus”)。

8fsztsew

8fsztsew2#

这办法对我也奏效了:

saajSoapMessage.setSoapAction("urn:server#getBoardStatus");

相关问题