java soap api操做和發送soap消息

Java代碼  收藏代碼java

  1. package gov.hn12396.appintegration.mule.client;  app

  2.   

  3. import gov.hn12396.appintegration.mule.util.EncoderUtil;  dom

  4.   

  5. import java.net.URL;  ide

  6. import java.util.Calendar;  測試

  7.   

  8. import javax.xml.soap.MessageFactory;  網站

  9. import javax.xml.soap.SOAPBody;  url

  10. import javax.xml.soap.SOAPConnection;  spa

  11. import javax.xml.soap.SOAPConnectionFactory;  .net

  12. import javax.xml.soap.SOAPElement;  code

  13. import javax.xml.soap.SOAPEnvelope;  

  14. import javax.xml.soap.SOAPMessage;  

  15. import javax.xml.soap.SOAPPart;  

  16. import javax.xml.transform.Source;  

  17. import javax.xml.transform.Transformer;  

  18. import javax.xml.transform.TransformerFactory;  

  19. import javax.xml.transform.stream.StreamResult;  

  20.   

  21. import org.w3c.dom.Node;  

  22.   

  23. /** 

  24.  * 功能描述:模擬客戶端A-即服務調用者,經過該類模擬客戶端發送soap報文給mule, 

  25.  * 同時把mule的響應報文打印出來作測試 

  26.  * @author liuxp 

  27.  * 

  28.  */  

  29. public class SynClient {  

  30.   

  31.     public static void main(String args[]) {  

  32.   

  33.         try {  

  34.   

  35.             // 建立鏈接  

  36.             // ==================================================  

  37.             SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory  

  38.                     .newInstance();  

  39.             SOAPConnection connection = soapConnFactory.createConnection();  

  40.   

  41.             //  建立消息對象  

  42.             // ===========================================  

  43.             MessageFactory messageFactory = MessageFactory.newInstance();  

  44.             SOAPMessage message = messageFactory.createMessage();  

  45. //          message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "gb2312");  

  46.   

  47.             // 建立soap消息主體==========================================  

  48.             SOAPPart soapPart = message.getSOAPPart();// 建立soap部分  

  49.             SOAPEnvelope envelope = soapPart.getEnvelope();  

  50.             SOAPBody body = envelope.getBody();  

  51.             //  根據要傳給mule的參數,建立消息body內容。具體參數的配置能夠參照應用集成接口技術規範1.1版本  

  52.             // =====================================  

  53.             SOAPElement bodyElement = body.addChildElement(envelope.createName(  

  54.                     "process""Request""http://esb.service.com/"));  

  55.             bodyElement.addChildElement("ServiceCode").addTextNode("10000061");  

  56.             bodyElement.addChildElement("OrigAppId").addTextNode("999");  

  57.             bodyElement.addChildElement("HomeAppId").addTextNode("998");  

  58.             Calendar c = Calendar.getInstance();  

  59.             String reqTime = String.valueOf(c.getTimeInMillis());  

  60.             bodyElement.addChildElement("ReqTime").addTextNode(reqTime);  

  61.             bodyElement.addChildElement("IpAddress").addTextNode("10.212.40.112");  

  62.             bodyElement.addChildElement("OrigSerialNo").addTextNode("201205242011");  

  63.             //(ServiceCode+ OrigAppId+ ReqTime+ IpAddress)簽名  

  64.             String AppSignature = "10000061"+"999"+reqTime+"10.212.40.112"+"123456";  

  65.             bodyElement.addChildElement("AppSignature").addTextNode(EncoderUtil.md5(AppSignature));  

  66.             bodyElement.addChildElement("Version").addTextNode("014");  

  67. //          bodyElement.addChildElement("RelSessionId").addTextNode("RelSessionId");  

  68. //          bodyElement.addChildElement("ReplyCode").addTextNode("ReplyCode");  

  69.             bodyElement.addChildElement("ReplyVersion").addTextNode("05");  

  70.             bodyElement.addChildElement("TimeOut").addTextNode("30");  

  71. //          bodyElement.addChildElement("FtpDir").addTextNode("FtpDir");  

  72. //          bodyElement.addChildElement("FileList").addTextNode("FileList");  

  73.             bodyElement.addChildElement("serviceParas").addTextNode("<param><name>apptest</name><password>apptest</password></param>");  

  74.             // Save the message  

  75.             message.saveChanges();  

  76.             // 打印客戶端發出的soap報文,作驗證測試  

  77.             System.out.println(" REQUEST: ");  

  78.             message.writeTo(System.out);  

  79.             System.out.println(" ");  

  80.             /* 

  81.              * 實際的消息是使用 call()方法發送的,該方法接收消息自己和目的地做爲參數,並返回第二個 SOAPMessage 做爲響應。 

  82.              * call方法的message對象爲發送的soap報文,url爲mule配置的inbound端口地址。 

  83.              */  

  84.             URL url = new URL("http://localhost:9003/WebServiceSyn/process");  

  85.             System.out.println(url);  

  86.             // 響應消息  

  87.             // ===========================================================================  

  88.             SOAPMessage reply = connection.call(message, url);  

  89.             //reply.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "gb2312");  

  90.             // 打印服務端返回的soap報文供測試  

  91.             System.out.println("RESPONSE:");  

  92.             // ==================建立soap消息轉換對象  

  93.             TransformerFactory transformerFactory = TransformerFactory  

  94.                     .newInstance();  

  95.             Transformer transformer = transformerFactory.newTransformer();  

  96.             // Extract the content of the reply======================提取消息內容  

  97.             Source sourceContent = reply.getSOAPPart().getContent();  

  98.             // Set the output for the transformation  

  99.             StreamResult result = new StreamResult(System.out);  

  100.             transformer.transform(sourceContent, result);  

  101.             // Close the connection 關閉鏈接 ==============  

  102.             System.out.println("");  

  103.             connection.close();  

  104.             /* 

  105.              * 模擬客戶端A,異常處理測試 

  106.              */  

  107.             SOAPBody ycBody = reply.getSOAPBody();  

  108.             Node ycResp = ycBody.getFirstChild();  

  109.             System.out.print("returnValue:"+ycResp.getTextContent());  

  110.         } catch (Exception e) {  

  111.             e.printStackTrace();  

  112.             System.out.println(e.getMessage());  

  113.         }  

  114.     }  

  115. }  

關注流行國外網站

facebook:http://www.fb-on.com

facebook官網:http://www.facebookzh.com

facebook×××:http://www.cn-face-book.com

youtube:http://www.youtubezh.com

twitter:http://www.twitterzh.com

相關文章
相關標籤/搜索