SoapMessage对象转换成字符串

下面的代码可以把一个SoapMessage对象转换成字符串对象

 

SOAPConnection conn = SOAPConnectionFactory.newInstance().createConnection();
SOAPMessage resMsg = conn.call(reqMsg, url);
Document doc = resMsg.getSOAPPart().getEnvelope().getOwnerDocument();
StringWriter output = new StringWriter();
TransformerFactory.newInstance().newTransformer().transform( new DOMSource(doc), new StreamResult(output));
System.out.println(output.toString());

 注:reqMsg也是一个SoapMessage对象,url是对方服务地址,意思就是把reqMsg消息发送到url

 

       resMsg是对方响应的SoapMessage对象,对方响应的代码如下:

 

int num = (int)(Math.random() * 10) % 2;
response.setContentType("text/xml; charset=utf-8");
response.setHeader("Connection", Global.KEEP_LIVE);
response.setHeader("User-Agent", "gSOAP/2.7");
SOAPMessage soap = Global.formartSoapString(PressureTool.createUploadResponse(num));
OutputStream os = response.getOutputStream();
soap.writeTo(os);
os.close();

 注:要想返回的消息是SoapMessage对象,上面response对象的三个set方法是必须的

你可能感兴趣的:(message)