码迷,mamicode.com
首页 > 编程语言 > 详细

Java Axis2 webservice

时间:2018-03-15 11:07:07      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:最好   rcc   action   catch   XML   sub   比较   post   esc   

 

 

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;

public class testAxis2 {

	public static void main(String[] args) {

		String url = "http://192.168.1.1/xxx/DTIService?wsdl";
		String namespace = "http://xxx.com.cn";
		String methodName = "processMessage";
		testDocument(url, namespace, methodName);

	}

	/**
	 * 方法二: 应用document方式调用 用ducument方式应用现对繁琐而灵活。现在用的比较多。因为真正摆脱了我们不想要的耦合
	 */
	public static void testDocument(String url, String namespace,
			String methodName) {
		try {

			String xmlData = "<ROOTMAIN><CASEMAIN><SUBTYPECODE>10101</SUBTYPECODE><EVENTGRADECODE>1</EVENTGRADECODE><EVENTID>2875715</EVENTID><DAMAGEGRADENAME>暂无</DAMAGEGRADENAME><EVENTDESC>北四环西路辅路与六郞庄路交叉口向北50米六郞庄路东侧有绿地脏乱</EVENTDESC><EVENTTYPECODE>1</EVENTTYPECODE><PARTCODE></PARTCODE><ADDRESS>六郎庄37号杆(东南20米)</ADDRESS><OPERATORNAME></OPERATORNAME><TASKNUM>16110803978</TASKNUM><CELLCODE>11010802300219</CELLCODE><TELNUM>13910351520</TELNUM><INSTDATE>2016-11-08T14:24:12</INSTDATE><STREETCODE>110108023</STREETCODE><CREATEDATE>2016-11-08T14:24:12</CREATEDATE><RECDISPNUM>2016海淀管字1793973</RECDISPNUM><MAINTYPECODE>1</MAINTYPECODE><EVENTSRCCODE>1</EVENTSRCCODE><COMMUNITYCODE>110108023002</COMMUNITYCODE><DISTRICTCODE>110108</DISTRICTCODE><PATROLNAME>杜文森</PATROLNAME><COORDINATEY>312853.0</COORDINATEY><COORDINATEX>494444.0</COORDINATEX></CASEMAIN><CASEOPERATIONDETAIL><SIGNHUMANNAME>冯文越</SIGNHUMANNAME><SIGNDATE>2016-11-11T08:51:13</SIGNDATE></CASEOPERATIONDETAIL></ROOTMAIN>";

			Options options = new Options();
			// 指定调用WebService的URL
			EndpointReference targetEPR = new EndpointReference(url);
			options.setTo(targetEPR);
			// options.setAction("urn:getPrice");

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

			OMFactory fac = OMAbstractFactory.getOMFactory();
			// 命名空间,有时命名空间不增加没事,不过最好加上,因为有时有事,你懂的
			OMNamespace omNs = fac.createOMNamespace(namespace, methodName);

			/* 以下设置添加SOAP消息头 */
			OMElement SenderCode = fac.createOMElement("SenderCode", omNs);
			OMElement ReceiverCode = fac.createOMElement("ReceiverCode", omNs);
			OMElement OperationCode = fac
					.createOMElement("OperationCode", omNs);
			OMElement MessageID = fac.createOMElement("MessageID", omNs);
			SenderCode.setText("002");
			ReceiverCode.setText("000");
			OperationCode.setText("011");
			MessageID.setText("10000000000000000");

			sender.addHeader(SenderCode);
			sender.addHeader(ReceiverCode);
			sender.addHeader(OperationCode);
			sender.addHeader(MessageID);

			/* 以下设置要发送的参数 */
			// 指定方法 名
			OMElement method = fac.createOMElement(methodName, omNs);
			// 指定方法的参数名
			OMElement symbol = fac.createOMElement("MessageInfo", omNs);
			symbol.setText(xmlData);
			// symbol.addChild(fac.createOMText(symbol, xmlData));
			method.addChild(symbol);
			method.build();

			OMElement res = sender.sendReceive(method);

			String result = res.getFirstElement().getText();
			System.out.println(result);

		} catch (AxisFault axisFault) {
			axisFault.printStackTrace();
		}
	}

}

  

Java Axis2 webservice

标签:最好   rcc   action   catch   XML   sub   比较   post   esc   

原文地址:https://www.cnblogs.com/chenghu/p/8571553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!