java直接调用WebServices接口

本文介绍了一种通过Java调用WebService接口的方法,详细展示了如何设置URL、SOAPAction、方法名及参数,并提供了完整的示例代码。同时,列出了所需的所有依赖库,包括Axis、commons-discovery等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据url 调用:http://10.220.105.60:8080/pdyjws/baseMethod.asmx

/**
     * 调用webservice接口,返回string类型的json值
     * @return
     */
    public String getWebServices(){
    	//获取webservice接口地址
        String url = "http://10.220.105.60:8080/pdyjws/baseMethod.asmx";
        //获取域名地址,server定义的
        String soapaction = "http://tempuri.org/";
        Service service = new Service();
        //调用的方法
        String method="getDailyFast";
        //调用的参数
        String startdate="2019-02-11";
        String enddate="2019-02-12";
        String result="";
        try {
              Call call = (Call) service.createCall();
              call.setTargetEndpointAddress(url);
              //设置要调用的方法
              call.setOperationName(new QName(soapaction,method));
              call.addParameter(new QName(soapaction, "startdate"), // 设置要传递的参数--要和接口方提供的参数名一致
                      org.apache.axis.encoding.XMLType.XSD_STRING,
                      javax.xml.rpc.ParameterMode.IN);
              call.addParameter(new QName(soapaction, "enddate"), // 设置要传递的参数
                      org.apache.axis.encoding.XMLType.XSD_STRING,
                      javax.xml.rpc.ParameterMode.IN);
              //设置要返回的数据类型
              call.setReturnType(new QName(soapaction,method), String.class);
              call.setUseSOAPAction(true);
              call.setSOAPActionURI(soapaction+method);
              //调用方法并传递参数
              result = (String) call.invoke(new Object[]{startdate,enddate});
             } catch (ServiceException e) {
            	 bislogger.error("[PdfDataTask][getWebServices]Exception1",e);
            } catch (Exception e) {
            	 bislogger.error("[PdfDataTask][getWebServices]Exception2",e);
            }
        return result;
        }

需要的jar包,这些jar包不能少:

<!-- https://mvnrepository.com/artifact/org.apache.axis/axis -->
		<dependency>
		    <groupId>org.apache.axis</groupId>
		    <artifactId>axis</artifactId>
		    <version>1.4</version>
		</dependency>
		<dependency>
		    <groupId>commons-discovery</groupId>
		    <artifactId>commons-discovery</artifactId>
		    <version>0.2</version>
		</dependency>
		<dependency>
		    <groupId>wsdl4j</groupId>
		    <artifactId>wsdl4j</artifactId>
		    <version>1.6.2</version>
		</dependency>
		<dependency>
		    <groupId>javax.xml</groupId>
		    <artifactId>jaxrpc</artifactId>
		    <version>1.1</version>
		</dependency>
		<dependency>
			<groupId>commons-httpclient</groupId>
			<artifactId>commons-httpclient</artifactId>
			<version>3.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
		<dependency>
		    <groupId>org.apache.httpcomponents</groupId>
		    <artifactId>httpclient</artifactId>
		    <version>4.5.7</version><!--$NO-MVN-MAN-VER$-->
		</dependency>
				<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
		<dependency>
		    <groupId>org.apache.httpcomponents</groupId>
		    <artifactId>httpmime</artifactId>
		    <version>4.5.7</version><!--$NO-MVN-MAN-VER$-->
		</dependency>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值