1.java soap api操作和發送soap消息


轉自: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     }  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM