Java的方法发送SOAP WebService

import java.net.URL;
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.SOAPHeader;
import javax.xml.soap.SOAPMessage;

import javax.xml.soap.SOAPPart;

public class SendSoapMessage
{
public static void buildSoapMessage()
{
try
{
SOAPConnectionFactory conntools=SOAPConnectionFactory.newInstance();
SOAPConnection conn=conntools.createConnection();
//创建消息工厂
MessageFactory factory=MessageFactory.newInstance();
//创建soap消息reqMsg
SOAPMessage reqMsg=factory.createMessage();
//创建soap消息的部分reqMsgpart
SOAPPart part=reqMsg.getSOAPPart();
//创建sope信封envelope,要开始写信了
SOAPEnvelope envelope=part.getEnvelope();
envelope.setAttribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
envelope.setAttribute("xmlns:esb", "http://ngbss.huawei.com/esb/endpointurl/webservice");
//写header
SOAPHeader header=envelope.getHeader();
//写Body
SOAPBody body=envelope.getBody();
SOAPBodyElement bodyElement=body.addBodyElement(envelope.createName("EndPointUrlRequestMsg", "esb", "http://ngbss.huawei.com/esb/endpointurl/webservice"));
SOAPBodyElement requestHeader=(SOAPBodyElement) bodyElement.addChildElement("RequestHeader", "esb");
SOAPBodyElement requestBody=(SOAPBodyElement) bodyElement.addChildElement("RequestBody", "esb");
SOAPBodyElement urls=(SOAPBodyElement) requestBody.addChildElement("urls", "esb");
SOAPBodyElement url=(SOAPBodyElement) urls.addChildElement("url", "esb");
//RquestHeader
requestHeader.addChildElement("userId", "esb").addTextNode("500");
requestHeader.addChildElement("password", "esb").addTextNode("500");
//RequestBody
urls.setAttribute("Action", "Add");
url.addChildElement("key", "esb").addTextNode("AR4CCInterfaceService");
url.addChildElement("pass", "esb").addTextNode("TRUE");
url.addChildElement("URL", "esb").addTextNode("http://10.137.30.66:9533/ar/ccinterface/wsdl/AR4CCInterfaceService.wsdl");
url.addChildElement("frondEnd", "esb").addTextNode("CRM");
url.addChildElement("backEnd", "esb").addTextNode("CBS");
url.addChildElement("soap", "esb").addTextNode("SOAP11");
url.addChildElement("timeOut", "esb").addTextNode("100");
URL endPoint =new URL("http://localhost:7080/Demo/service");
System.out.println("\n====================发送的消息:");
reqMsg.writeTo(System.out);
SOAPMessage respMsg = conn.call(reqMsg, endPoint);
// System.out.println("\n服务端返回的信息- : " + getResult(respMsg));
System.out.println("\n\n====================接收的消忿");
respMsg.writeTo(System.out);
ReceiverSoapMessage.receiverMessage(respMsg);
System.out.print("Success!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值