注释 是自己的理解。。
package com.jielan.axis2;
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 Client01 {
private static String url = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx";
//端点引用 指接口位置
private static EndpointReference targetEpr = new EndpointReference(url);
//有抽象OM工厂获取OM工厂,创建request SOAP包
private static OMFactory fac = OMAbstractFactory.getoMFactory();
public static OMElement getoMMethod(String methodStr,String namespace,String tns,String[] pars,String[] vals){
//创建命名空间
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;
}
public static Options getClientOptions(String action){
//创建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;
}
OMElement result = null;
try {
ServiceClient client = new ServiceClient();
client.setoptions(getClientOptions(action));
result = client.sendReceive(getoMMethod(methodStr,namespace,tns,pars,vals));
} catch (AxisFault e) {
// Todo Auto-generated catch block
e.printstacktrace();
}
return result;
}
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(action,methodStr,vals);
System.out.println(result);
}
}
查看namespace
查看 端点引用 指接口位置 接口地址
另外OMElement method = fac.createOMElement(methodStr,nms);第二个参数nms 具体是什么好像随便填,""也行
不过查了很多看到有人说这是服务名。。
这里只要不填已有的变量名,遇到变量重复定义错误,就都可以。。
结果:
index.jsp上调用
<body>
<%
//out.print(InvokeExercise.invokeWucan().replaceAll("</string>","</string>\r<br/>"));
//out.print(InvokeExercise.invokeYouCan());
out.print(Client01.getWeather(
"http://WebXml.com.cn/getWeatherbyCityName",
"getWeatherbyCityName","http://WebXml.com.cn/",
"xsd",new String[]{"theCityName"},
new String[]{"台州"}).toString().replaceAll("。","。<br/>").replaceAll("今日天气实况","<br/>今日天气实况"));
%>
</body>
web上调用 结果 :
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。