1.java soap api操作和发送soap消息[通俗易懂]

发布时间:2025-12-09 13:56:09 浏览次数:4

转自:https://blog.csdn.net/lbinzhang/article/details/84721359

1。

 1 /**  2  * soap请求  3  *  4  * @return  5  * @throws Exception  6 */  7 public static String invokeMethod(Object data) throws Exception {  8 // 创建连接   9 SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance(); 10 SOAPConnection soapConn = soapConnFactory.createConnection(); 11 // 创建消息对象  12 MessageFactory messageFactory = MessageFactory.newInstance(); 13 SOAPMessage soapMessage = messageFactory.createMessage(); 14 // 创建soap消息主体  15 SOAPPart soapPart = soapMessage.getSOAPPart(); 16 SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); 17 SOAPBody body = soapEnvelope.getBody(); 18 // 根据要传给mule的参数,创建消息body内容  19 SOAPElement bodyElement =body.addChildElement(soapEnvelope.createName("amsPortal", "ns2875","http://tempuri.org")); 20 bodyElement.addChildElement("secret").addTextNode("true"); 21 bodyElement.addChildElement("command").addTextNode("OracleCli"); 22 SOAPElement argsElement = bodyElement.addChildElement("args"); 23 argsElement.addChildElement("ConnStr").addTextNode("192.168.40.175:1521/orcl"); 24 argsElement.addChildElement("User").addTextNode("mtis"); 25 argsElement.addChildElement("Pass").addTextNode("mtis"); 26  soapMessage.saveChanges(); 27 28 // soapMessage = saveSoapChage(data, soapEnvelope, body, soapMessage);  29 /* 30  * 实际的消息是使用 call()方法发送的,该方法接收消息本身和目的地作为参数,并返回第二个 SOAPMessage 作为响应。 31  * call方法的message对象为发送的soap报文,url为mule配置的inbound端口地址。 32 */ 33 URL url = new URL("http://192.168.200.236/soap/soap_server_pro.php"); 34 // 响应消息  35 SOAPMessage reply = soapConn.call(soapMessage, url); 36 // 创建soap消息转换对象  37 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 38 Transformer transformer = transformerFactory.newTransformer(); 39 // 提取消息内容  40 Source sourceContent = reply.getSOAPPart().getContent(); 41 //输出流  42 ByteArrayOutputStream out = new ByteArrayOutputStream(); 43 StreamResult result = new StreamResult(out); 44 //sourceContent实现此接口的对象包含充当源输入(XML 源或转换指令)所需的信息 45 //result充当转换结果的持有者,可以为 XML、纯文本、HTML 或某些其他格式的标记  46  transformer.transform(sourceContent, result); 47 //返回结果  48 String xmlData = new String(out.toByteArray()); 49 // xml解析  50 xmlData = parserXml(data, xmlData); 51 //输出到控制台  52  System.out.println(xmlData); 53 //关闭连接  54  soapConn.close(); 55 return xmlData; 56 } 
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477