package gov.hn12396.appintegration.mule.client; app
import gov.hn12396.appintegration.mule.util.EncoderUtil; dom
import java.net.URL; ide
import java.util.Calendar; 測試
import javax.xml.soap.MessageFactory; 網站
import javax.xml.soap.SOAPBody; url
import javax.xml.soap.SOAPConnection; spa
import javax.xml.soap.SOAPConnectionFactory; .net
import javax.xml.soap.SOAPElement; code
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Node;
/**
* 功能描述:模擬客戶端A-即服務調用者,經過該類模擬客戶端發送soap報文給mule,
* 同時把mule的響應報文打印出來作測試
* @author liuxp
*
*/
public class SynClient {
public static void main(String args[]) {
try {
// 建立鏈接
// ==================================================
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory
.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();
// 建立消息對象
// ===========================================
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
// message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "gb2312");
// 建立soap消息主體==========================================
SOAPPart soapPart = message.getSOAPPart();// 建立soap部分
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();
// 根據要傳給mule的參數,建立消息body內容。具體參數的配置能夠參照應用集成接口技術規範1.1版本
// =====================================
SOAPElement bodyElement = body.addChildElement(envelope.createName(
"process", "Request", "http://esb.service.com/"));
bodyElement.addChildElement("ServiceCode").addTextNode("10000061");
bodyElement.addChildElement("OrigAppId").addTextNode("999");
bodyElement.addChildElement("HomeAppId").addTextNode("998");
Calendar c = Calendar.getInstance();
String reqTime = String.valueOf(c.getTimeInMillis());
bodyElement.addChildElement("ReqTime").addTextNode(reqTime);
bodyElement.addChildElement("IpAddress").addTextNode("10.212.40.112");
bodyElement.addChildElement("OrigSerialNo").addTextNode("201205242011");
//(ServiceCode+ OrigAppId+ ReqTime+ IpAddress)簽名
String AppSignature = "10000061"+"999"+reqTime+"10.212.40.112"+"123456";
bodyElement.addChildElement("AppSignature").addTextNode(EncoderUtil.md5(AppSignature));
bodyElement.addChildElement("Version").addTextNode("014");
// bodyElement.addChildElement("RelSessionId").addTextNode("RelSessionId");
// bodyElement.addChildElement("ReplyCode").addTextNode("ReplyCode");
bodyElement.addChildElement("ReplyVersion").addTextNode("05");
bodyElement.addChildElement("TimeOut").addTextNode("30");
// bodyElement.addChildElement("FtpDir").addTextNode("FtpDir");
// bodyElement.addChildElement("FileList").addTextNode("FileList");
bodyElement.addChildElement("serviceParas").addTextNode("<param><name>apptest</name><password>apptest</password></param>");
// Save the message
message.saveChanges();
// 打印客戶端發出的soap報文,作驗證測試
System.out.println(" REQUEST: ");
message.writeTo(System.out);
System.out.println(" ");
/*
* 實際的消息是使用 call()方法發送的,該方法接收消息自己和目的地做爲參數,並返回第二個 SOAPMessage 做爲響應。
* call方法的message對象爲發送的soap報文,url爲mule配置的inbound端口地址。
*/
URL url = new URL("http://localhost:9003/WebServiceSyn/process");
System.out.println(url);
// 響應消息
// ===========================================================================
SOAPMessage reply = connection.call(message, url);
//reply.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "gb2312");
// 打印服務端返回的soap報文供測試
System.out.println("RESPONSE:");
// ==================建立soap消息轉換對象
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
// Extract the content of the reply======================提取消息內容
Source sourceContent = reply.getSOAPPart().getContent();
// Set the output for the transformation
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
// Close the connection 關閉鏈接 ==============
System.out.println("");
connection.close();
/*
* 模擬客戶端A,異常處理測試
*/
SOAPBody ycBody = reply.getSOAPBody();
Node ycResp = ycBody.getFirstChild();
System.out.print("returnValue:"+ycResp.getTextContent());
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
關注流行國外網站
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