使用Axis2生成soap客户端

来源:互联网 发布:cv2.calchist python 编辑:程序博客网 时间:2024/05/19 18:37
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");//创建一个两层的bodyOMElement 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;}


原创粉丝点击