Java用axis2调用.net发布的web services接口
来源:互联网 发布:http php 上传文件 编辑:程序博客网 时间:2024/05/17 08:01
一、下载Axis2源码包,下载地址为:http://axis.apache.org/axis2/java/core/download.html
二、将所需要的jar包导入到工程里面,下面列出的是用到的jar包:
三、创建调用接口的测试类,目前接触到的为两种调用方法,分别如下:
1.用ServiceClient的方式来调用,代码如下:
package com.test;/** * Created with IntelliJ IDEA. * User: like * Date: 2016/1/19 * Time: 14:20 * Description: 用axis2调用.net发布的web services接口 * Version: v1.0 */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;import org.apache.axis2.transport.http.HTTPConstants;public class TestClient { public static void main(String[] args) { String url = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx"; String action = "http://WebXml.com.cn/getWeatherbyCityName"; String methodStr = "getWeatherbyCityName"; String namespace = "http://WebXml.com.cn/"; String tns = "xsd"; String[] pars = {"theCityName"}; String[] vals = {"杭州"}; OMElement result = null; result = getWeather(url,action, methodStr, namespace, tns, pars, vals); System.out.println(result); } public static OMElement getWeather(String url,String action,String methodStr,String namespace,String tns,String[] pars,String[] vals){ OMElement result = null; try { ServiceClient client = new ServiceClient(); client.setOptions(getClientOptions(url,action)); result = client.sendReceive(getOMMethod(methodStr,namespace,tns,pars,vals)); } catch (AxisFault e) { e.printStackTrace(); } return result; } public static Options getClientOptions(String url,String action){ //端点引用 指接口位置 EndpointReference targetEpr = new EndpointReference(url); //创建request soap包 请求选项 Options options = new Options(); //设置options的soapAction options.setAction(action); //设置request soap包的端点引用(接口地址) options.setTo(targetEpr); //如果报错提示Content-Length,请求内容长度 options.setProperty(HTTPConstants.CHUNKED,"false");//把chunk关掉后,会自动加上Content-Length。 return options; } public static OMElement getOMMethod(String methodStr,String namespace,String tns,String[] pars,String[] vals){ //有抽象OM工厂获取OM工厂,创建request SOAP包 OMFactory fac = OMAbstractFactory.getOMFactory(); //创建命名空间 OMNamespace nms = fac.createOMNamespace(namespace, tns); //创建OMElement方法 元素,并指定其在nms指代的名称空间中 OMElement method = fac.createOMElement(methodStr, nms); //添加方法参数名和参数值 for(int i=0, tns); //创建OMElement方法 元素,并指定其在nms指代的名称空间中 OMElement method = fac.createOMElement(methodStr, nms); //添加方法参数名和参数值 for(int i=0;i<pars.length;i++){ //创建方法参数OMElement元素 OMElement param = fac.createOMElement(pars[i],
package com.test;/** * Created with IntelliJ IDEA. * User: like * Date: 2016/1/19 * Time: 14:20 * Description: 用axis2调用.net发布的web services接口 * Version: v1.0 */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;import org.apache.axis2.transport.http.HTTPConstants;public class TestClient { public static void main(String[] args) { String url = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx"; String action = "http://WebXml.com.cn/getWeatherbyCityName"; String methodStr = "getWeatherbyCityName"; String namespace = "http://WebXml.com.cn/"; String tns = "xsd"; String[] pars = {"theCityName"}; String[] vals = {"杭州"}; OMElement result = null; result = getWeather(url,action, methodStr, namespace, tns, pars, vals); System.out.println(result); } public static OMElement getWeather(String url,String action,String methodStr,String namespace,String tns,String[] pars,String[] vals){ OMElement result = null; try { ServiceClient client = new ServiceClient(); client.setOptions(getClientOptions(url,action)); result = client.sendReceive(getOMMethod(methodStr,namespace,tns,pars,vals)); } catch (AxisFault e) { e.printStackTrace(); } return result; } public static Options getClientOptions(String url,String action){ //端点引用 指接口位置 EndpointReference targetEpr = new EndpointReference(url); //创建request soap包 请求选项 Options options = new Options(); //设置options的soapAction options.setAction(action); //设置request soap包的端点引用(接口地址) options.setTo(targetEpr); //如果报错提示Content-Length,请求内容长度 options.setProperty(HTTPConstants.CHUNKED,"false");//把chunk关掉后,会自动加上Content-Length。 return options; } public static OMElement getOMMethod(String methodStr,String namespace,String tns,String[] pars,String[] vals){ //有抽象OM工厂获取OM工厂,创建request SOAP包 OMFactory fac = OMAbstractFactory.getOMFactory(); //创建命名空间 OMNamespace nms = fac.createOMNamespace(namespace, tns); //创建OMElement方法 元素,并指定其在nms指代的名称空间中 OMElement method = fac.createOMElement(methodStr, nms); //添加方法参数名和参数值 for(int i=0;i<pars.length;i++){ //创建方法参数OMElement元素 OMElement param = fac.createOMElement(pars[i],nms); //设置键值对 参数值 param.setText(vals[i]); //讲方法元素 添加到method方法元素中 method.addChild(param); } return method; }}
运行结果如下图所示:
2.用RPCServiceClient方式调用,代码如下:
package com.test;/** * Created with IntelliJ IDEA. * User: like * Date: 2016/1/19 * Time: 15:09 * Description: * Version: v1.0 */import java.util.ArrayList;import java.util.Iterator;import java.util.List;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.Constants;import org.apache.axis2.addressing.EndpointReference;import org.apache.axis2.client.Options;import org.apache.axis2.rpc.client.RPCServiceClient;public class TestClient2 { public static void main(String[] args) throws AxisFault { String url = "http://fy.webxml.com.cn/webservices/EnglishChinese.asmx?WSDL"; String action = "http://WebXml.com.cn/TranslatorString"; String methodStr = "TranslatorString"; String namespace = "http://WebXml.com.cn/"; String tns = ""; String[] pars = {"wordKey"}; String[] vals = {"随便"}; OMElement result = null; result = translatorString(url,action, methodStr, namespace, tns, pars, vals); System.out.println(getResults(result)); //System.out.println(result); } public static OMElement translatorString(String url,String action,String methodStr,String namespace,String tns,String[] pars,String[] vals){ OMElement result = null; try { RPCServiceClient serviceClient = new RPCServiceClient(); serviceClient.setOptions(getClientOptions(url,action)); result = serviceClient.sendReceive(getOMMethod(methodStr,namespace,tns,pars,vals)); } catch (AxisFault e) { e.printStackTrace(); } return result; } public static Options getClientOptions(String url,String action){ //有抽象OM工厂获取OM工厂,创建request SOAP包 EndpointReference targetEpr = new EndpointReference(url); //创建request soap包 请求选项 Options options = new Options(); //设置options的soapAction options.setAction(action); //设置request soap包的端点引用(接口地址) options.setTo(targetEpr); //设置超时时间 options.setTimeOutInMilliSeconds(6000000000L); //传输协议 options.setTransportInProtocol(Constants.TRANSPORT_HTTP); return options; } public static OMElement getOMMethod(String methodStr,String namespace,String tns,String[] pars,String[] vals){ //有抽象OM工厂获取OM工厂,创建request SOAP包 OMFactory fac = OMAbstractFactory.getOMFactory(); //创建命名空间 OMNamespace nms = fac.createOMNamespace(nms); //设置键值对 参数值 param.setText(vals[i]); //讲方法元素 添加到method方法元素中 method.addChild(param); method.build(); } return method; } //解析XML,将获取到的数据封装到list中 public static List<String> getResults(OMElement element) { if (element == null) { return null; } Iterator iterator = element.getChildElements(); Iterator innerItr; List<String> list = new ArrayList<String>(); OMElement result = null; while (iterator.hasNext()) { result = (OMElement) iterator.next(); innerItr = result.getChildElements(); while(innerItr.hasNext()){ OMElement result2 = (OMElement)innerItr.next(); if(result2!=null){ String text = result2.getText(); if(text!=null && !("").equals(text)){ list.add(text); } } } } return list; }}
运行结果如下:
1 0
- Java用axis2调用.net发布的web services接口
- Java使用Axis2调用Web services接口(二)
- Web Services学习3——用Axis2发布Web Services的方法
- java调用.net的web services
- 详解 Web service Axis2 JAVA调用.net接口 返回String的xml格式数据
- 用Java做客户端调用.NET写的 Web Services
- Java Axis2发布及调用webservice接口
- axis2发布web项目开发webservice和接口调用
- .Net中访问Axis2下的Web Services
- Java与.NET 的Web Services相互调用
- 利用WSDL2JAVA实现JAVA调用.Net的Web Services(1)
- Java与.NET 的Web Services相互调用
- Java与.NET 的Web Services相互调用
- Java与.NET 的Web Services相互调用 (一)
- Java与.NET 的Web Services相互调用(二)
- Java与.NET 的Web Services相互调用
- Java与.NET 的Web Services相互调用
- Java与.NET 的Web Services相互调用
- Java并发编程:Callable、Future和FutureTask
- Android6.0的phone应用源码分析(5)——RIL层分析
- 留训第三天
- decltype的使用
- [Android] LogFilterM 发布啦
- Java用axis2调用.net发布的web services接口
- Ligerui表格基本操作(四)
- MuseScore 的编译
- SQL Server中的四舍五入函数ROUND
- HTTP上传文件到WEB服务器
- Windows+Linux双系统时删除Linux系统
- poj1456
- 万恶的流氓首页~~我要干掉你
- Android Studio设置或修改SDK路径