Java调用Webservice接口(.asmx后缀)

1、浏览器直接访问接口地址,获取接口服务器都而命名空间名称和方法名称,接口中要用

targetNamespace
method

2、代码

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

public class SendMessage {
    
    
    public static void main(String[] args) throws Exception {
    
    
        String strMobile="123132123132";
        String messageContent="测试";
        QingDaoSendMessage(strMobile,messageContent);
    }


    /**
     *
     * 此接口为Webservice接口
     * @param strMobile 
     * @param messageContent 要发送的手机内容
     */
    public static void QingDaoSendMessage(String strMobile,String messageContent){
    
    
        //ip地址
        String url = "http://sss.qdfd.gov.cn/sms/test/sms.asmx";
        //服务命名空间
        String targetNamespace = "http://qdfd.org/";
        //服务方法名
        String method = "SendMessageData";
        try {
    
    
            String endpoint = url;
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new java.net.URL(endpoint));
            QName opAddEntry = new QName(targetNamespace, method);
            call.setOperationName(opAddEntry);
            call.setUseSOAPAction(true);
            call.addParameter(new QName(targetNamespace, "strMobile"), XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter(new QName(targetNamespace, "messageContent"), XMLType.XSD_STRING, ParameterMode.IN);
            call.setReturnType(XMLType.SOAP_DOCUMENT);
            call.setSOAPActionURI(targetNamespace + method);
            Object[] params = new Object[]{
    
    strMobile,messageContent};
            Object o = call.invoke(params);
            System.out.println(o);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }
}

3、注意返回的参数类型,否则会报错。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41033385/article/details/109506139
今日推荐