Java调用C#WebService接口方式

package test;

import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.encoding.XMLType;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class MyTestPurchase {

    String result="";
        String url = "http://locahost:8008/myWebService.asmx?wsdl";  
        String namespace = "http://tempuri.org/";  
        String methodName = "ReportCatalog";  
        String soapActionURI = "http://tempuri.org/ReportCatalog";  
        Service service = new Service();  
        Call call;  
        try {  
            call = (Call) service.createCall();  
            call.setTargetEndpointAddress(url);  
            call.setUseSOAPAction(true);  
            call.setSOAPActionURI(soapActionURI);  
            call.setOperationName(new QName(namespace, methodName));  
            call.addParameter(new QName(namespace, "medicineName"), XMLType.XSD_STRING,ParameterMode.IN);
            call.addParameter(new QName(namespace, "pageNo"), XMLType.XSD_STRING,ParameterMode.IN);
            call.addParameter(new QName(namespace, "pageSize"), XMLType.XSD_STRING,ParameterMode.IN);
            call.setReturnType(XMLType.XSD_STRING);  
            //对传入的参数进行赋值操作
            String[] str = new String[3];  
            str[0] = medicineName;  
            str[1] = pageNo;  
            str[2] = pageSize;  
            //开始调用服务
            Object obj = call.invoke(str);  
            result = (String)obj;
            System.out.println(obj);  //对返回参数进行输出
        } catch (ServiceException e) {  
            e.printStackTrace();  
        } catch (RemoteException e) {  
            e.printStackTrace();  
        }
        return result;

    public static void main(String[] args) {
        String result=doPP();  

    }
}

整个代码如上,此方法试用于对webService服务没有进行修改格式,如果对文件格式进行了自己的约束,调用如下:

        String url = "http://localhost:9000/services/cmcc_mas_wbs?wsdl";
        //主要区别在于整个namespace,按照接口的wsdl文件查看对应值进行更改即可,参数的传递都是一样的  
        String namespace = "http://www.csapi.org/service";  
        String methodName = "sendSms";  
        String soapActionURI = "http://www.csapi.org/service/sendSms";

就这些,有什么疑问或是不合理的地方,请各位看官老爷及时指正!

你可能感兴趣的:(Java调用C#WebService接口方式)