axis2調用webservice的三種方法,並有相關的jar包!

三種方法親測有效!

package com.qz.axis;

import javax.xml.namespace.QName;

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.rpc.client.RPCServiceClient;

public class TestAxis2 {

	public static  final String targetEndpoint = "http://10.209.16.4:8991/MWBusinessModel/services/CMDataService";
	public static  final String targetNamespace = "http://info.nari-china.com/CMS";
	public static final String method = "getIceThicknessDataList";
	
	
	public static void main(String[] args) {
		try {
			send();
		} catch (AxisFault e) {
			e.printStackTrace();
		}
	}
	
	
	
	public static String send() throws AxisFault {
		 RPCServiceClient client = new RPCServiceClient();
		 Options options = client.getOptions();
		 
		 options.setTo(new EndpointReference(targetEndpoint));
		 
		 options.setTimeOutInMilliSeconds(1000 * 60 * 5);// 毫秒單位
		 options.setAction(method);
		 
		 String ss = " 1=1 and to_date(to_char(ACQUISITION_TIME,'yyyy/mm/dd HH24:mi:ss'),'yyyy/mm/dd HH24:mi:ss') between to_date('2019/01/15 00:00:00','yyyy/mm/dd HH24:mi:ss') and to_date('2019/02/20 00:00:00','yyyy/mm/dd HH24:mi:ss')";
		 int pageCount = 2000;
		 int pageIndex = 0;
		 Object[] response =  client.invokeBlocking(new QName(targetNamespace, method), new Object[]{ss.toString(), String.valueOf(pageCount), String.valueOf(pageIndex)}, new Class[]{String.class});
		 String results = (String) response[0];
	     return results;
	}
	
	public static void weather() throws AxisFault{
		ServiceClient serviceClient = new ServiceClient();
		//創建服務地址WebService的URL,注意不是WSDL的URL
        String url = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx";
        EndpointReference targetEPR = new EndpointReference(url);
        Options options = serviceClient.getOptions();
        options.setTo(targetEPR);
      //確定調用方法(wsdl 命名空間地址 (wsdl文檔中的targetNamespace) 和 方法名稱 的組合)
        options.setAction("http://WebXml.com.cn/getRegionProvince");
        
        OMFactory fac = OMAbstractFactory.getOMFactory();
        /*
         * 指定命名空間,參數:
         * uri--即爲wsdl文檔的targetNamespace,命名空間
         * perfix--可不填
         */
        OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/", "");
        // 指定方法
        OMElement method = fac.createOMElement("getSupportCityDataset", omNs);
        // 指定方法的參數
        OMElement theRegionCode = fac.createOMElement("theRegionCode", omNs);
        theRegionCode.setText("北京");
        method.addChild(theRegionCode);
        method.build();

        //遠程調用web服務
        OMElement result = serviceClient.sendReceive(method);
        System.out.println(result);
	}
	
	
	public void MobileCodeWS() {
        try {
            ServiceClient serviceClient = new ServiceClient();
            //創建服務地址WebService的URL,注意不是WSDL的URL
            String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx";
            EndpointReference targetEPR = new EndpointReference(url);
            Options options = serviceClient.getOptions();
            options.setTo(targetEPR);
            //確定調用方法(wsdl 命名空間地址 (wsdl文檔中的targetNamespace) 和 方法名稱 的組合)
            options.setAction("http://WebXml.com.cn/getMobileCodeInfo");

            OMFactory fac = OMAbstractFactory.getOMFactory();
            /*
             * 指定命名空間,參數:
             * uri--即爲wsdl文檔的targetNamespace,命名空間
             * perfix--可不填
             */
            OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/", "");
            // 指定方法
            OMElement method = fac.createOMElement("getMobileCodeInfo", omNs);
            // 指定方法的參數
            OMElement mobileCode = fac.createOMElement("mobileCode", omNs);
            mobileCode.setText("15932582632");
            OMElement userID = fac.createOMElement("userID", omNs);
            userID.setText("");
            method.addChild(mobileCode);
            method.addChild(userID);
            
            method.build();

            //遠程調用web服務
            OMElement result = serviceClient.sendReceive(method);
            System.out.println(result);

        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
        }
    }

}

下載地址:https://download.csdn.net/download/qq_39444339/11006503

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