Axis2 調用wsdl webservice 示例

package com.sinosoft.webservice;

import java.util.Date;

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.axiom.soap.SOAP11Constants;
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.client.ServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.HttpTransportProperties.ProxyProperties;

public class WeatherAxiomClient {

 private static EndpointReference targetEPR = 
        new EndpointReference("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");
 
 private static OMFactory fac = OMAbstractFactory.getOMFactory();
 static OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/", "tns");
 
 public static void main(String args[]) throws AxisFault{
  Date start=new Date();
        System.out.println("start:"+start);
        ServiceClient sender = new ServiceClient();
        
       
        
        sender.setOptions(buildOptions("http://WebXml.com.cn/getWeatherbyCityName"));
        OMElement result = sender.sendReceive(buildParam("getWeatherbyCityName",new String[]{"theCityName"},new String[]{"上海"}));
        
        System.out.println(result);
        Date end=new Date();
        System.out.println("end:"+end);
        System.out.println("between:"+(end.getTime()-start.getTime())); 
 }
 /**
  * @see 調用webservice得到天氣預報支持的城市
  * @return
  */
 public static  OMElement buildParam(String method,String[] arg,String[] val) {
        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;

 }
 

 /**
  * @see 設置連接屬性
  * @return
  */
 public static Options  buildOptions(String action){
  Options options = new Options();
        options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
        //options.setSoapVersionURI(action);
        options.setAction("http://WebXml.com.cn/getSupportCity");
        options.setTo(targetEPR);
        options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
        options.setProperty(HTTPConstants.CHUNKED, "false");//設置不受限制.
        options.setProperty(HTTPConstants.PROXY, buildProxy());
        options.setProperty(Constants.Configuration.HTTP_METHOD,HTTPConstants.HTTP_METHOD_POST);
       // options.setAction(action);
        return options;

 }
 
 /**
  * @see 設置代理屬性
  * @return
  */
 
 public static ProxyProperties buildProxy(){
     ProxyProperties proxyProperties=new ProxyProperties();
        proxyProperties.setProxyName("172.19.18.22");
        proxyProperties.setProxyPort(8080);
        return proxyProperties;
 }
 
 
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章