Web Service

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());
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值