欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java实现WebService的客户端SOAP方式访问

程序员文章站 2022-05-18 15:06:09
...

pom.xml

	<!-- https://mvnrepository.com/artifact/org.apache.axis/axis -->
		<dependency>
			<groupId>org.apache.axis</groupId>
			<artifactId>axis</artifactId>
			<version>1.4</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.axis/axis-jaxrpc -->
		<dependency>
			<groupId>org.apache.axis</groupId>
			<artifactId>axis-jaxrpc</artifactId>
			<version>1.4</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/commons-discovery/commons-discovery -->
		<dependency>
			<groupId>commons-discovery</groupId>
			<artifactId>commons-discovery</artifactId>
			<version>0.5</version>
		</dependency>
	<!-- https://mvnrepository.com/artifact/wsdl4j/wsdl4j -->
		<dependency>
			<groupId>wsdl4j</groupId>
			<artifactId>wsdl4j</artifactId>
			<version>1.6.2</version>
		</dependency>

 

java

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 Main1 {
    public static void main(String[] args) {
        Main1 main1 = new Main1();
        main1.doService();
    }

    final String Url = "http://192.168.199.160:8089/myservice.asmx";
    final String namespace = "http://com.soft.ws/my";
    final String method = "readJson";
    final String paraName = "userId";
    final String paraValue = "admin";
    public void doService() {
        try {
            Service service = new Service();
            Call call = (Call) service.createCall();
            //访问路径
            call.setTargetEndpointAddress(Url);
            //访问的方法名称
            call.setOperationName(new QName(namespace,method));
            //访问参数
            call.addParameter(new QName(namespace,paraName), XMLType.XSD_STRING, ParameterMode.IN);
            //使用SOAP方式请求
            call.setUseSOAPAction(true);
            //设置请求的路径
            call.setSOAPActionURI(namespace + method);
            //设置返回结果类型
            call.setReturnType(XMLType.XSD_STRING);
            //开始执行,并获取结果
            Object obj = call.invoke(new Object[] {paraValue});
            System.out.println(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

打印结果:hello

创建webservice请参考 https://blog.csdn.net/linjinhuo/article/details/78777694

如果不知道要访问的targetNamespace,请打开?wsdl查看。抬头就有。包括方法名和参数

java实现WebService的客户端SOAP方式访问

相关标签: webservice