使用Axis2生成soap客户端

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

/**
 * 发送soap工具类
 * 
 * @author wpj_service
 *
 */
public class soapClient {
	private static final String url = "127.0.0.1:80";
	/**
	 * 
	 * @return 
	 * @author wpj
	 * @throws axisFault
	 */
	public static String sendSoap() {
		String result = "";
		try {
			// ---------------------头-----------------------------------------------
			OMFactory factory = OMAbstractFactory.getOMFactory();

			OMNamespace SecurityElementNamespace = factory.createOMNamespace(
					"http://www.baidu.com.cn/common/header/in", "in");
			OMElement soapHeader = factory.createOMElement("soapHeader",
					SecurityElementNamespace);

			OMNamespace SecurityElementNamespace1 = factory.createOMNamespace(
					"http://www.w3.org/2001/XMLSchema-instance", "xsi");
			soapHeader.declareNamespace(SecurityElementNamespace1);

			OMElement head1 = factory.createOMElement("msgRef",
					SecurityElementNamespace);
			head1.setText("head1");

			soapHeader.addChild(head1);
			// ---------------------头end----------------------------------------------

			// ---------------------体-------------------------------------------------
			OMNamespace omNsBody = factory.createOMNamespace(
					"http://schemas.xmlsoap.org/soap/envelope/", "soap-env");
			OMElement soapBody = factory.createOMElement("Body", omNsBody);

			OMNamespace omNsTargetNs = factory.createOMNamespace(
					"http://www.baidu.com.cn/card",
					"targetNs");
			OMNamespace omNsHeader2 = factory.createOMNamespace(
					"http://schemas.xmlsoap.org/soap/envelope/", "soap");
			soapBody.declareNamespace(omNsTargetNs);
			soapBody.declareNamespace(omNsHeader2);
			soapBody.declareNamespace(omNsBody);

			OMElement soapTargetNs = factory.createOMElement(
					"doProcessRequest", omNsTargetNs);

			OMElement body1 = factory.createOMElement("serviceCode",
					omNsTargetNs);
			body1.setText("body1");
			
			//创建一个两层的body
			OMElement body2 = factory.createOMElement(
					"transactionDetails", omNsTargetNs);

			OMElement body21 = factory
					.createOMElement("phoneNo", omNsTargetNs);
			body21.setText("body21");
			
			body2.addChild(body21);
		
			soapTargetNs.addChild(body1);
			soapTargetNs.addChild(body2);
			// ---------------------体end------------------------

			Options options = new Options();
			EndpointReference targetEPR = new EndpointReference(url);

			options.setTo(targetEPR);
			options.setAction("targetNs:doProcessRequest");

			ServiceClient sender = new ServiceClient();
			sender.setOptions(options);

			sender.addHeader(soapHeader);

			OMElement reOME = sender.sendReceive(soapTargetNs);

			result = reOME.toString();
			
		} catch (AxisFault axisFault) {
			axisFault.printStackTrace();
		}
		return result;
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值