如何将Soap请求发送到webservice

时间:2014-06-04 06:17:43

标签: java web-services soap

我是Soap的新手,

在我发送的SOAPUI的帮助下

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<SOAP-ENV:Header/>
<soapenv:Body>
  <tem:RequestData>
     <tem:requestDocument>
     <![CDATA[
     <Request>
       <Authentication CMId="68" Function="1" Guid="5594FB83-F4D4-431F-B3C5-EA6D7A8BA795" Password="poihg321TR"/>
       <Establishment Id="4297867"/>
    </Request>
    ]]>
    </tem:requestDocument>
  </tem:RequestData>
 </soapenv:Body>
</soapenv:Envelope>  

SOAPUI的响应是

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
  <RequestDataResponse xmlns="http://tempuri.org/">
     <RequestDataResult><![CDATA[<Response>
 <Authentication CMId="68" Function="1" Guid="5594FB83-F4D4-431F-B3C5-EA6D7A8BA795" Password="poihg321TR" />
 <Establishment Id="4297867">
  <RoomTypes>
  <RoomType RoomTypeId="1040459" Description="Double Room" />
  <RoomType RoomTypeId="1040458" Description="Single Room" />
</RoomTypes>
  <BoardTypes>
   <BoardType BoardTypeId="1" Description="Room Only" />
  </BoardTypes>
</Establishment>
</Response>]]></RequestDataResult>
  </RequestDataResponse>
 </s:Body>
</s:Envelope>  

现在我已经编写了一个Java代码

            MessageFactory mf = MessageFactory.newInstance();
        SOAPMessage sm = mf.createMessage();
        SOAPEnvelope envelope = sm.getSOAPPart().getEnvelope();
        envelope.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelope/");
        envelope.setPrefix("soapenv");

        envelope.setAttribute("xmlns:tem", "http://tempuri.org/");

        SOAPBody body = envelope.getBody();
        body.setPrefix("soapenv");

        SOAPElement requestData = body.addChildElement("RequestData");
        requestData.setPrefix("tem");

        SOAPElement requestDoc = requestData.addChildElement("requestDocument", "tem", "http://tempuri.org/");



        CDATASection cdata = requestDoc.getOwnerDocument().createCDATASection("<Request>\n"
                + "           <Authentication CMId=\"68\" Function=\"1\" Guid=\"5594FB83-F4D4-431F-B3C5-EA6D7A8BA795\" Password=\"poihg321TR\"/>\n"
                + "           <Establishment Id=\"4297867\"/>\n"
                + "        </Request>");
        requestDoc.appendChild(cdata);
        sm.writeTo(System.out);


        SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
        SOAPConnection connection = sfc.createConnection();
        URL requestUrl = new URL("http://pp.hotels.travelrepublic.co.uk/ChannelManager.svc?wsdl");
        SOAPMessage response = connection.call(sm, requestUrl);
        System.out.println("");
        System.out.println("***************");
        response.writeTo(System.out);

从java代码发送的请求(与soap ui发送的请求相同)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"><SOAP-ENV:Header/><soapenv:Body><tem:RequestData><tem:requestDocument xmlns:tem="http://tempuri.org/"><![CDATA[<Request>
       <Authentication CMId="68" Function="1" Guid="5594FB83-F4D4-431F-B3C5-EA6D7A8BA795" Password="poihg321TR"/><Establishment Id="4297867"/>
    </Request>]]></tem:requestDocument></tem:RequestData></soapenv:Body></soapenv:Envelope>    

但我得到的回应是

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><s:Fault><faultcode xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">a:ActionNotSupported</faultcode><faultstring xml:lang="en-GB">The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver.  Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).</faultstring></s:Fault></s:Body></s:Envelope>

请帮助我是webservices的新手

2 个答案:

答案 0 :(得分:0)

您的请求中似乎缺少SOAP Action

        SOAPBody body = envelope.getBody();
    body.setPrefix("soapenv");

    SOAPHeader header = envelope.getHeader();       
    header.addHeader("SOAPAction", <add your URL>");

您可以尝试设置SOAP Action标头吗?

答案 1 :(得分:0)

我认为名称空间对你起了作用:

变化

SOAPElement requestDoc = requestData.addChildElement("requestDocument", "tem", "http://tempuri.org/");

SOAPElement requestDoc = requestData.addChildElement("requestDocument");