java soap 操作和发送soap消息

  1. package gov.hn12396.appintegration.mule.client;  
  2.   
  3. import gov.hn12396.appintegration.mule.util.EncoderUtil;  
  4.   
  5. import java.net.URL;  
  6. import java.util.Calendar;  
  7.   
  8. import javax.xml.soap.MessageFactory;  
  9. import javax.xml.soap.SOAPBody;  
  10. import javax.xml.soap.SOAPConnection;  
  11. import javax.xml.soap.SOAPConnectionFactory;  
  12. import javax.xml.soap.SOAPElement;  
  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. }  
posted @ 2013-03-28 13:18  奋斗的小菜鸟  阅读(6786)  评论(1编辑  收藏  举报