Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何向webservice发送Soap请求_Java_Web Services_Soap - Fatal编程技术网

Java 如何向webservice发送Soap请求

Java 如何向webservice发送Soap请求,java,web-services,soap,Java,Web Services,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/">

我不熟悉肥皂

在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新手

您的请求中似乎缺少SOAP操作

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

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

您可以尝试设置SOAP操作头吗?

我认为名称空间在捉弄您:

改变

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


SOAPElement requestDoc=requestData.addChildElementrequestDocument

找不到symbol:methodaddHeaderString,string如果我这样更改,我就不会得到相同的xmlrequest。我试过,但问题是一样的