java 访问。net webservice

java访问。net的webservice 而且 返回值是一个类,这玩意折磨我一天,出现过一系列的问题,

包括 没有定义SoapAction 不能序列化类等等。 最后还是服务提供商给了个demo,解决了 ,原因是我返回自定义类的一个变量没有get函数,可耻啊。而且demo里的代码 我想也是用wsdd2java生成的,我以前也看到过这种模式的实现,看着费劲。

整理了一下,如下:

private static String endpoint = "http://***/SmsService.asmx";
    private static String nameSpace = "http://***.org/";

 

               Service service = new Service();
                call = (Call) service.createCall();

                 // 定义服务器地址
                call.setTargetEndpointAddress(new java.net.URL(endpoint));

                 // 定义SOAPAction
                call.setUseSOAPAction(true);
                call.setSOAPActionURI(nameSpace + "SendMessage");

                //定义访问方法名称
                call.setOperationName(new QName(nameSpace, "SendMessage"));

                //定义返回类型 SendState为序列化类,注意一定要和服务器定义的包一样
                QName qn = new QName(nameSpace, "SendState");

                 //注册返回值类
                 call.registerTypeMapping(SendState.class, qn,
                        new BeanSerializerFactory(SendState.class, qn),
                        new BeanDeserializerFactory(SendState.class, qn));

                //添加参数一共4个参数

                call.addParameter(new QName(nameSpace, "UserId"),
                        Constants.XSD_STRING, ParameterMode.IN);
                call.addParameter(new QName(nameSpace, "Password"),
                        Constants.XSD_STRING, ParameterMode.IN);
                call.addParameter(new QName(nameSpace, "Msg"),
                        Constants.XSD_STRING, ParameterMode.IN);
                call.addParameter(new QName(nameSpace, "Destnumbers"),
                        Constants.XSD_STRING, ParameterMode.IN);

                call.setReturnType(qn);

                 //调用

                SendState so = (SendState) call.invoke(new Object[] { userid,
                    password, msg, destnumbers });
            System.out.println(so.getState());
            System.out.println(so.getWrongNumbers());
            System.out.println(so.getLTGroupId());
            System.out.println(so.getYDGroupId());

 

 

 

/ SendState自定义类

public class SendState  implements java.io.Serializable {
    private int state;

    private java.lang.String wrongNumbers;

    private java.lang.String YDGroupId;

    private java.lang.String LTGroupId;

    public SendState() {
    }

    public SendState(
           int state,
           java.lang.String wrongNumbers,
           java.lang.String YDGroupId,
           java.lang.String LTGroupId) {
           this.state = state;
           this.wrongNumbers = wrongNumbers;
           this.YDGroupId = YDGroupId;
           this.LTGroupId = LTGroupId;
    }


    /**
     * Gets the state value for this SendState.
     *
     * @return state
     */
    public int getState() {
        return state;
    }


    /**
     * Sets the state value for this SendState.
     *
     * @param state
     */
    public void setState(int state) {
        this.state = state;
    }


    /**
     * Gets the wrongNumbers value for this SendState.
     *
     * @return wrongNumbers
     */
    public java.lang.String getWrongNumbers() {
        return wrongNumbers;
    }


    /**
     * Sets the wrongNumbers value for this SendState.
     *
     * @param wrongNumbers
     */
    public void setWrongNumbers(java.lang.String wrongNumbers) {
        this.wrongNumbers = wrongNumbers;
    }


    /**
     * Gets the YDGroupId value for this SendState.
     *
     * @return YDGroupId
     */
    public java.lang.String getYDGroupId() {
        return YDGroupId;
    }


    /**
     * Sets the YDGroupId value for this SendState.
     *
     * @param YDGroupId
     */
    public void setYDGroupId(java.lang.String YDGroupId) {
        this.YDGroupId = YDGroupId;
    }


    /**
     * Gets the LTGroupId value for this SendState.
     *
     * @return LTGroupId
     */
    public java.lang.String getLTGroupId() {
        return LTGroupId;
    }


    /**
     * Sets the LTGroupId value for this SendState.
     *
     * @param LTGroupId
     */
    public void setLTGroupId(java.lang.String LTGroupId) {
        this.LTGroupId = LTGroupId;
    }





    // Type metadata
    private static org.apache.axis.description.TypeDesc typeDesc =
        new org.apache.axis.description.TypeDesc(SendState.class, true);

    static {
        typeDesc.setXmlType(new javax.xml.namespace.QName("http://tempuri.org/", "SendState"));
        org.apache.axis.description.ElementDesc elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("state");
        elemField.setXmlName(new javax.xml.namespace.QName("http://tempuri.org/", "state"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "int"));
        elemField.setNillable(false);
        typeDesc.addFieldDesc(elemField);
        elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("wrongNumbers");
        elemField.setXmlName(new javax.xml.namespace.QName("http://tempuri.org/", "WrongNumbers"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
        elemField.setMinOccurs(0);
        elemField.setNillable(false);
        typeDesc.addFieldDesc(elemField);
        elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("YDGroupId");
        elemField.setXmlName(new javax.xml.namespace.QName("http://tempuri.org/", "YDGroupId"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
        elemField.setMinOccurs(0);
        elemField.setNillable(false);
        typeDesc.addFieldDesc(elemField);
        elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("LTGroupId");
        elemField.setXmlName(new javax.xml.namespace.QName("http://tempuri.org/", "LTGroupId"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
        elemField.setMinOccurs(0);
        elemField.setNillable(false);
        typeDesc.addFieldDesc(elemField);
    }

    /**
     * 就差这一步啊 否则 org.xml.sax.SAXException: Invalid element ---------错误
     */
    public static org.apache.axis.description.TypeDesc getTypeDesc() {
        return typeDesc;
    }

  
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值