WebService之客户端访问服务端样例

public class SynOrgUserClient {
    //以下是写了一个测试方法,对其进行测试;

    public static void main(String[] args) {

   //所需参数:webserviceUrl地址;命名空间;访问方法名;参数名;参数值;

        String wsdlUrl = "http://localhost:8080/xxxx/services/SynOrgUserService?wsdl";
        String targetNamespace = "http://amsuri.org/";
        String methodName = "trans_InstitutionalUsersInfo";
        String parmName = "requestXml";//requestXml均可,此为形参;
        String parmValue = "";
        //---------------以下是xml测试案例,为拼接xml字符串而写,适度参考-----------------------------------
        SAXReader sax = new SAXReader();// 创建一个SAXReader对象
        File xmlFile = new File("d:\\user.xml");// 根据指定的路径创建file对象
        Document document = null;
        try {
            document = sax.read(xmlFile);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }// 获取document对象,如果文档无节点,则会抛出Exception提前结束
        Element root = document.getRootElement();// 获取根节点
        String rootStr = root.asXML();
        //------------------------------------------------------------------------------------------------------
        try {
            Object obj = callWs(wsdlUrl,targetNamespace,methodName,parmName,rootStr);
            System.out.println(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Object callWs(String wsdlUrl,String targetNamespace,String methodName,String parmName,String parmValue) throws Exception{
        Object res = "";
        try{           
            QName qName = org.apache.axis.Constants.XSD_STRING;
            
            ParameterDesc param = new ParameterDesc(new QName(targetNamespace, parmName), ParameterDesc.IN, qName, java.lang.String.class, false, false);
            param.setNillable(true);
            OperationDesc oper = new OperationDesc();
            oper.setName(methodName);
            oper.addParameter(param);
            oper.setReturnType(qName);
            oper.setStyle(org.apache.axis.constants.Style.WRAPPED);
            oper.setUse(org.apache.axis.constants.Use.LITERAL);
            
            Service service = new Service();
            Call _call = (Call) service.createCall();
            _call.setTargetEndpointAddress(wsdlUrl);
            _call.setOperation(oper);
            _call.setUseSOAPAction(true);
            String actionURI = targetNamespace.endsWith("/")?targetNamespace:targetNamespace+"/";
            _call.setSOAPActionURI(actionURI+methodName);
            _call.setEncodingStyle(null);
            _call.setProperty(org.apache.axis.client.Call.SEND_TYPE_ATTR, Boolean.FALSE);
            _call.setProperty(org.apache.axis.AxisEngine.PROP_DOMULTIREFS, Boolean.FALSE);
            _call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
            _call.setOperationName(new QName(targetNamespace, methodName));
            _call.setTimeout(60000);
            res = _call.invoke(new Object[]{parmValue});
        }catch(Exception e){
            throw new Exception("调用WebService失败,错误信息:"+e.getMessage());      
        }
        return res;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值