1. 程式人生 > >介面程式設計(webservice和post)

介面程式設計(webservice和post)

package com;

import org.apache.axiom.om.OMElement;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.Iterator;

public class Client {
    //

    public static void main(String[] args) throws RemoteException, ServiceException, MalformedURLException {
        /* **************** 呼叫方法1 *********************** */

        RPCServiceClient rpcClient = new RPCServiceClient();
    Options opt = new Options();
    opt.setTo(new EndpointReference("http://localhost:8080/axis2/services/AXIS"));
    opt.setAction("urn:axis1");
    rpcClient.setOptions(opt);
    OMElement element = rpcClient.invokeBlocking(new QName("http://com.axis", "axis1"), new Object[]{null});
    System.out.println(element);
    System.out.println(element.getFirstElement().getText());
    Iterator value = element.getChildElements();
    while(value.hasNext()){
    OMElement ome = (OMElement)value.next();
    System.out.println(ome.getText());
    }

         /* **************** 呼叫方法2 *********************** */

        String method = "hw";
        Service service = new Service();
        Call call = (Call) service.createCall();
        call.setTargetEndpointAddress(new java.net.URL("http://localhost:8080/axis2/services/Hello"));
        call.setOperationName(new QName("http://com/", method));
        call.setUseSOAPAction(true);
        call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
        call.setSOAPActionURI("http://com/GetServerList");
        String k = (String)call.invoke(new Object[]{}); //因為返回值是String型別,所以這裡呼叫的返回值也是String型別

        System.out.println(">>> "+k); //返回值輸出

    }
}