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

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查看。抬头就有。包括方法名和参数

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Java WebService 客户端示例代码。示例中通过调用 WebService 提供的方法来实现获取天气信息的功能。 1. 首先,需要导入相关的 jar 包,例如 `jaxws-api.jar` 和 `javax.xml.soap.jar`。 2. 创建一个包含 WebService 方法调用的类,例如 `WeatherClient`: ```java import java.net.URL; import javax.xml.namespace.QName; import javax.xml.soap.*; import javax.xml.ws.*; import org.w3c.dom.NodeList; public class WeatherClient { public static void main(String[] args) { try { // 创建 WebService 客户端对象 URL wsdlURL = new URL("http://www.example.com/WeatherService?wsdl"); QName serviceName = new QName("http://www.example.com/", "WeatherService"); Service service = Service.create(wsdlURL, serviceName); // 获取 WebService 接口 QName portName = new QName("http://www.example.com/", "WeatherPort"); WeatherInterface weatherInterface = service.getPort(portName, WeatherInterface.class); // 调用 WebService 方法 String city = "Beijing"; String weather = weatherInterface.getWeather(city); System.out.println("The weather in " + city + " is " + weather); } catch (Exception e) { e.printStackTrace(); } } } ``` 3. 创建一个接口,用于定义 WebService 提供的方法,例如 `WeatherInterface`: ```java import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface WeatherInterface { @WebMethod public String getWeather(String city); } ``` 4. 最后,在 WebService 客户端代码中调用接口中的方法即可获得天气信息。 以上就是一个简单的 Java WebService 客户端示例。通过调用 WebService 提供的方法,可以获取到相关的天气信息。注意在实际开发中,需要根据具体的 WebService 接口进行适当的调整和处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值