import java.net.URL;
import java.util.List;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import org.apache.log4j.Logger;
public class SendSoapMessage
{
private static Logger logger = Logger.getLogger(SendSoapMessage.class);
// 连接对象
private static SOAPConnection conn = null;
// 创建消息工厂
private static MessageFactory msgfactory = null;
// 创建soap消息reqMsg
private static SOAPMessage reqMsg = null;
private static SOAPEnvelope envelope = null;
// Messge Body的对象
private static SOAPBody body = null;
/**
* 初始化
*/
public static void init()
{
logger.debug("init SOAP Connection Factory begin...");
try
{
// 构建SOAP 连接工厂
SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();
// 得到连接
conn = factory.createConnection();
logger.debug("init SOAP Connection Factory sueecss.");
}
catch (Exception e)
{
logger
.error("init SOAP Connection Factory failed and The Message Was:"
+ e.getMessage());
}
}
/**
* 构建SOAP头的信息
*/
public static void buildSOAPMessageHeader()
{
logger.debug("build SOAP Message Header begin...");
try
{
// 给信息工厂赋值
msgfactory = MessageFactory.newInstance();
reqMsg = msgfactory.createMessage();
// 创建soap消息的部分reqMsgpart
SOAPPart part = reqMsg.getSOAPPart();
// 创建sope信封envelope,要开始写信了
envelope = part.getEnvelope();
// 给定前缀
envelope.setPrefix("soapenv");
// 定义命名空间
envelope.setAttribute("xmlns:soapenv",
"http://schemas.xmlsoap.org/soap/envelope/");
envelope.setAttribute("xmlns:esb",
"http://ngbss.huawei.com/esb/endpointurl/webservice");
// 写header
envelope.getHeader().setPrefix("soapenv");
// 写Body
envelope.getBody().setPrefix("soapenv");
// 定义SOAPBody
body = envelope.getBody();
logger.debug("build SOAP Message Header sueecss." + reqMsg);
}
catch (Exception e)
{
logger
.error("build SOAP Message Header failed and The Message Was:"
+ e.getMessage());
}
}
import java.util.List;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import org.apache.log4j.Logger;
public class SendSoapMessage
{
private static Logger logger = Logger.getLogger(SendSoapMessage.class);
// 连接对象
private static SOAPConnection conn = null;
// 创建消息工厂
private static MessageFactory msgfactory = null;
// 创建soap消息reqMsg
private static SOAPMessage reqMsg = null;
private static SOAPEnvelope envelope = null;
// Messge Body的对象
private static SOAPBody body = null;
/**
* 初始化
*/
public static void init()
{
logger.debug("init SOAP Connection Factory begin...");
try
{
// 构建SOAP 连接工厂
SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();
// 得到连接
conn = factory.createConnection();
logger.debug("init SOAP Connection Factory sueecss.");
}
catch (Exception e)
{
logger
.error("init SOAP Connection Factory failed and The Message Was:"
+ e.getMessage());
}
}
/**
* 构建SOAP头的信息
*/
public static void buildSOAPMessageHeader()
{
logger.debug("build SOAP Message Header begin...");
try
{
// 给信息工厂赋值
msgfactory = MessageFactory.newInstance();
reqMsg = msgfactory.createMessage();
// 创建soap消息的部分reqMsgpart
SOAPPart part = reqMsg.getSOAPPart();
// 创建sope信封envelope,要开始写信了
envelope = part.getEnvelope();
// 给定前缀
envelope.setPrefix("soapenv");
// 定义命名空间
envelope.setAttribute("xmlns:soapenv",
"http://schemas.xmlsoap.org/soap/envelope/");
envelope.setAttribute("xmlns:esb",
"http://ngbss.huawei.com/esb/endpointurl/webservice");
// 写header
envelope.getHeader().setPrefix("soapenv");
// 写Body
envelope.getBody().setPrefix("soapenv");
// 定义SOAPBody
body = envelope.getBody();
logger.debug("build SOAP Message Header sueecss." + reqMsg);
}
catch (Exception e)
{
logger
.error("build SOAP Message Header failed and The Message Was:"
+ e.getMessage());
}
}