您的位置:首页 > 其它

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

2018-02-10 14:09 417 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: