根据访问路径和域名调用webService接口

如果客户未提供wsdl文件,也没有提供直接的访问链接,如直接访问某个页面告知应该如何调用改接口,则可

以根据文档的说明,自己编写相关的调用方法,配置调用方法,配置传递参数,同样可以完成webService接口

的调用,相比较根据wsdl直接生成客户端而言,编写起来有点麻烦,设置相关参数等信息稍微麻烦。不过如果

遇到这样的接口文档还是要按照这样去编写。

测试访问域名:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

import java.util.Vector;

 

import javax.xml.namespace.QName;

 

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

 

public class Main {

           public static void main(String[] args) {

              System.out.println("-----------------------------------步骤开始");

             // 接口的地址

             String url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";

 

             // 域名

             String soapaction = "http://WebXml.com.cn/";

             String City = "北京";

 

             Service service = new Service();

 

             try {

 

                          Call call = (Call) service.createCall();

 

                          call.setTargetEndpointAddress(url);

 

                          // 设置要调用哪个方法

                          call.setOperationName(new QName(soapaction, "getWeatherbyCityName"));

 

                          // 设置要传递的参数

                          call.addParameter(new QName(soapaction, "theCityName"),                                                                       org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);

 

                          // 要返回的数据类型(自定义类型)

                          call.setReturnType(new QName(soapaction, "getWeatherbyCityName"), Vector.class);

                          call.setUseSOAPAction(true);

                          call.setSOAPActionURI(soapaction + "getWeatherbyCityName");

 

                          Vector v = (Vector) call.invoke(new Object[] { City });// 调用方法并传递参数

 

                          for (int i = 0; i < v.size(); i++) {

 

                                                    System.out.println(v.get(i));

                          }

 

                          } catch (Exception ex) {

                               ex.printStackTrace();

                          }

                          System.out.println("-----------------------------------步骤结束");

                          }

}

猜你喜欢

转载自13851619632.iteye.com/blog/2359119