Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.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对web服务的Soap请求_Java_Web Services_Soap_Request - Fatal编程技术网

JAVA对web服务的Soap请求

JAVA对web服务的Soap请求,java,web-services,soap,request,Java,Web Services,Soap,Request,我需要通过Java将此请求发送到web服务: > <soapenv:Envelope > xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" > xmlns:gen="http://www.bossmedia.com/wsdl/genericportaladapter"> > <soapenv:Header/> <soapenv:Body> >

我需要通过Java将此请求发送到web服务:

> <soapenv:Envelope
> xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
> xmlns:gen="http://www.bossmedia.com/wsdl/genericportaladapter">   
> <soapenv:Header/>    <soapenv:Body>
>       <gen:GetPlayerDetails>
>          <request>
>             <systemUID>?</systemUID>
>             <sessionID>?</sessionID>
>          </request>
>       </gen:GetPlayerDetails>    
</soapenv:Body> </soapenv:Envelope>
>xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/"
>xmlns:gen=”http://www.bossmedia.com/wsdl/genericportaladapter">   
>     
>       
>          
>             ?
>             ?
>          
>           
最好的方法是什么,并将响应保存为计算机上的XML文件


最好的方法是什么?如果你发布一些有帮助的链接,我会很高兴的。我知道这是一个很受欢迎的问题,但我发现的一切都不适合我。

JDK文档合理地告诉您如何做到这一点,但它确实涉及到一些困难,因此这里有一些示例代码让您开始

如果您打算经常这样做,您可能希望将其封装在一些实用程序类中,以大大简化这一过程

注: 我不确定这段代码是否完美,但它为您提供了谷歌的所有正确信息

祝你好运

MessageFactory messageFactory = MessageFactory.newInstance();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();

Document document = null; // load your document from somewhere

// make your request message
SOAPMessage requestMessage = messageFactory.createMessage();

/// copy your message into the soap message
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
DOMResult result = new DOMResult(requestMessage.getSOAPBody());
transformer.transform(source, result);

requestMessage.saveChanges();


// make the SOAP call
URL endpoint = new URL("http://example.com/endpoint");
SOAPConnection connection = sfc.createConnection();
SOAPMessage responseMessage = connection.call(requestMessage, endpoint);

// do something with the response message
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
responseMessage.writeTo(outputStream);
System.out.println(new String(outputStream.toByteArray()));