Axis2 根据wsdl制作客户端程序

阅读更多
package test;

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;

/**
 * 测试axis2 客户端程序编写
 * @author Administratr
 *
 */
public class TestSoap {

	private static EndpointReference targetEPR = new EndpointReference(
	"http://www.xxxxx.net:8308/soapapi/service.php");//保密一下好了..免得伤及无辜

	
	
	/**
	 * 这是第一个方法 
	 * @return
	 */
	public static OMElement getParamInfoMessage() {
		OMFactory factory = OMAbstractFactory.getOMFactory();
		OMNamespace omNs = factory.createOMNamespace("http://opi.ims.nortelnetworks.com", "");
		OMElement id = factory.createOMElement("id", omNs);//第一个参数
		OMElement item = factory.createOMElement("item", omNs);//第二个参数
		id.setText("353781010215414");//给第一个参数赋值
		item.setText("STime");//给第二个参数赋值

		OMElement requestSoapMessage = factory.createOMElement("getParaItemInfo", omNs);//这里是调用的方法名字
		requestSoapMessage.addChild(id);//把第一个参数放进去
		requestSoapMessage.addChild(item);//放第二个参数
		requestSoapMessage.build();//这个一定要有
		
		return requestSoapMessage;
	}
	
	/**
	 * 这是第二个方法 
	 * @return
	 */
	public static OMElement setParamInfoMessage() {
		OMFactory factory = OMAbstractFactory.getOMFactory();
		OMNamespace omNs = factory.createOMNamespace("http://opi.ims.nortelnetworks.com", "");
		OMElement id = factory.createOMElement("id", omNs);
		OMElement item = factory.createOMElement("item", omNs);
		OMElement value = factory.createOMElement("value", omNs);
		id.setText("353781010215414");
		item.setText("STime");
		value.setText("234");

		OMElement requestSoapMessage = factory.createOMElement("setParaItemInfo", omNs);
		requestSoapMessage.addChild(id);
		requestSoapMessage.addChild(item);
		requestSoapMessage.addChild(value);
		requestSoapMessage.build();
		
		return requestSoapMessage;
	}
	
	/**
	 * 这是第三个
	 * @return
	 */
	public static OMElement getSoapRequestMessage() {
		OMFactory factory = OMAbstractFactory.getOMFactory();
		OMNamespace omNs = factory.createOMNamespace("http://opi.ims.nortelnetworks.com", "");
		OMElement id = factory.createOMElement("id", omNs);
		id.setText("353781010215414");

		OMElement requestSoapMessage = factory.createOMElement("getOrigData", omNs);
		requestSoapMessage.addChild(id);
		requestSoapMessage.build();
		
		return requestSoapMessage;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//先是创建这个东东
		OMElement requestSoapMessage = getParamInfoMessage();//第一个
//		OMElement requestSoapMessage = setParamInfoMessage();//第二个
//		OMElement requestSoapMessage = getParamInfoMessage();//第三个
		
		Options options = new Options();
		options.setTo(targetEPR);//这个是发送的wsdl位置,看wsdl那个xml的最下面那行的location
		
		ServiceClient sender = null;
		try {
			sender = new ServiceClient();
			sender.setOptions(options);
			OMElement ome=sender.sendReceive(requestSoapMessage);//发送
			
			System.out.println("**************************************************************");
			System.out.println("ome:"+ome);//接收
			System.out.println("**************************************************************\n");
		} catch (AxisFault e) {
			e.printStackTrace();
		}
	}
}


都写在注释里了...

你可能感兴趣的:(Apache,PHP,.net,XML)