最近有项目要用java调用webservice,而且服务端用的是.net。走了许多弯路。最后通过axis2实现功能。
代码部分上半部分是java代码,下半部分是maven的pom文件:
//endpoint 为 webserviceurl GetInfoByTypeAndArea 为方法名
EndpointReference targetEPR = new EndpointReference(endpoint);
Options options = new Options();
options.setAction("http://tempuri.org/GetInfoByTypeAndArea");// 调用接口方法
options.setTo(targetEPR);
options.setProperty(HTTPConstants.CHUNKED, "false");// 设置不受限制.
ServiceClient sender = null;
try {
sender = new ServiceClient();
sender.setOptions(options);
OMFactory fac = OMAbstractFactory.getOMFactory();
//-----------------------------复杂配置start------------------------------------------
OMNamespace omNs = fac.createOMNamespace("http://tempuri.org/", "");//设置命名空间
OMElement method = fac.createOMElement("GetInfoByTypeAndArea", omNs);//设置方法名称
OMElement type = fac.createOMElement("type", omNs), staX = fac.createOMElement("staX", omNs),
staY = fac.createOMElement("staY", omNs), endX = fac.createOMElement("endX", omNs),
endY = fac.createOMElement("endY", omNs), strwhere = fac.createOMElement("strwhere", omNs);//创建所有参数
//为参数赋值
type.setText("12"); method.addChild(type); staX.setText("0");
method.addChild(staX); staY.setText("1"); method.addChild(staY);
endX.setText("0"); method.addChild(endX); endY.setText("1");
method.addChild(endY); strwhere.setText("1"); method.addChild(strwhere);
method.build();
//-----------------------------复杂配置end------------------------------------------
System.out.println("method:" + method.toString());
OMElement response = sender.sendReceive(method);
System.out.println("response:" + response);
System.out.println("cityCode:" + response.getFirstElement().getText());
} catch (AxisFault e) { System.out.println("Error"); }
org.apache.axis2
axis2
${axis2.version}
pom
compile
org.apache.axis2
axis2-adb
${axis2.version}
jar
compile
org.apache.axis2
axis2-kernel
${axis2.version}
jar
compile
org.apache.axis2
axis2-transport-local
${axis2.version}
org.apache.axis2
axis2-transport-http
${axis2.version}
本人是用来进行学习交流,QQ:914423503