java调用webservice的.asmx接口

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

接触到一个.asmx结尾的webservice接口,为了增加记忆决定记录下来。


一、接口类型

已.asmx结尾的接口
例:接口地址:http://IP地址/xxx/service/xxx/xxxx.asmx
	方法名:test
	参数类型:string 

二、使用步骤

1.访问方式

代码如下(示例):

controller类:

String s = clientUtil.test("http://IP地址/xxx/service/xxx/xxxx.asmx", "test","test");

调用类:

public static String test(String Url, String methodName, String str) throws Exception {
        String ref = null;
        // webService链接地址
        String url = Url;
        //获取域名地址,server定义的
        String soapaction = "http://tempuri.org/";
		
        Service service = new Service();
        try {
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(url);
            // 设置要调用哪个方法
            call.setOperationName(new QName(soapaction, methodName));
            // 设置要传递的参数名
           call.addParameter(new QName(soapaction,"str"),org.apache.axis.encoding.XMLType.XSD_STRING,
       javax.xml.rpc.ParameterMode.IN);
            // 提供标准类型 有addParameter就必须有setReturnType
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
            call.setUseSOAPAction(true);
            call.setSOAPActionURI(soapaction + methodName);
            // 调用方法并传递参数
			ref = (String) call.invoke(new Object[]{str});
           return ref;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ref;
    }

2.导入的maven

如下:

<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <groupId>org.apache.axis</groupId>
    <artifactId>axis</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>axis</groupId>
    <artifactId>axis-jaxrpc</artifactId>
    <version>1.4</version>
</dependency>

参考:
[1]https://blog.csdn.net/qq_34302802/article/details/101197464

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值