一 使用axis调用webservice
1.导入包
activation.jar
axis.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
jaxrpc.jar
mail.jar
wsdl4j-1.5.1.jar
2.代码
private void getWeather(){ try{ // String url = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl"; String url="http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx"; String city="北京"; //命名空间 String soapaction="http://WebXml.com.cn/"; Service service = new Service(); Call call=(Call) service.createCall();
call.setTargetEndpointAddress(url); //方法名 // call.setOperationName(new QName(soapaction,"getSupportCityString")); 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.addParameter(new QName(soapaction,"theRegionCode"), org.apache.axis.encoding.XMLType.XSD_STRING, // javax.xml.rpc.ParameterMode.IN); //返回类型 call.setReturnType(new QName(soapaction,"getWeatherbyCityName"),Vector.class); // call.setReturnType(new QName(soapaction,"getSupportCityString"),Vector.class); call.setUseSOAPAction(true); call.setSOAPActionURI(soapaction+"getWeatherbyCityName"); // call.setSOAPActionURI(soapaction+"getSupportCityString"); 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(); } }
二 使用axis2调用webservice
1.导包
axiom-api-1.2.11.jar
axiom-impl-1.2.11.jar
axis2-adb-1.5.5.jar
axis2-kernel-1.5.5.jar
axis2-transport-http-1.5.5.jar
axis2-transport-local-1.5.5.jar
commons-codec-1.3.jar
commons-httpclient-3.1.jar
commons-io.jar
commons-logging-1.1.1.jar
httpcore-4.0.jar
mail-1.4.jar
neethi-2.0.5.jar
wsdl4j-1.6.2.jar
XmlSchema-1.4.3.jar2.代码
这里可以有两种方法实现
其一 手动构造OMElement对象然后通过ServiceClient发送接收(网上很多实例)
其二 通过读取本地的xml文件将其转换为OMElement 然后再发送接收
private static void invokeWebService() { try { String soapBindingAddress = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl"; EndpointReference endpointReference = new EndpointReference(soapBindingAddress); //创建一个OMFactory,下面的namespace、方法与参数均需由它创建 // OMFactory factory = OMAbstractFactory.getOMFactory(); //指定命名空间 // OMNamespace namespace = factory.createOMNamespace("http://WebXml.com.cn/", "web"); /* //下面创建一个method对象,"getSupportCityString"为方法名 OMElement method = factory.createOMElement("getSupportCityString", namespace); OMElement value1 = factory.createOMElement("theRegionCode", namespace);//方法的第一个参数名称 //手动构造参数和值 下面有一个buildParma方法 value1.addChild(factory.createOMText(value1, "北京"));//设定参数的值 // value1.setText("北京"); method.addChild(value1);//方法设置参数*/ //请求参数设置 Options options = new Options(); options.setAction("http://WebXml.com.cn/getSupportCityString"); options.setTo(endpointReference); ServiceClient sender = new ServiceClient(); sender.setOptions(options); //发送并得到结果,至此,调用成功,并得到了结果 // OMElement result = sender.sendReceive(buildParam("getSupportCityString",new String[]{"theRegionCode"},new String[]{"北京"})); // OMElement med=buildParam("getSupportCityString",new String[]{"theRegionCode"},new String[]{"北京"}); // System.out.println(med.toString()); OMElement methods =getStrToOme("E://test.xml"); OMElement result = sender.sendReceive(methods); //下面的输出结果为 System.out.println(result.toString()); } catch (AxisFault ex) { ex.printStackTrace(); } }
public static OMElement getStrToOme(String filename){ String xmlStr; OMElement msg=null; try { xmlStr = FileUtils.readFileToString(new File(filename)); String encoding="UTF-8"; msg=toOMElement(xmlStr, encoding); System.out.println(msg.toString()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return msg; }
public static OMElement toOMElement(String xmlStr, String encoding) { OMElement xmlValue; try { xmlValue = new StAXOMBuilder(new ByteArrayInputStream(xmlStr .getBytes(encoding))).getDocumentElement(); return xmlValue; } catch (Exception e) { return null; } }
/** * @see 调用webservice得到天气预报支持的城市 * @return */ public static OMElement buildParam(String method,String[] arg,String[] val) { OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/", "web"); OMElement data = fac.createOMElement(method, omNs); for(int i=0;i<arg.length;i++){ OMElement inner = fac.createOMElement(arg[i], omNs); inner.setText(val[i]); data.addChild(inner); } return data; }