一个简单的webservice客户端demo

首先我们是客户端,我们要找一个服务端来配合我们调试,可以在这个网址里找个你喜欢的免费服务WEB服务(Web Servicrs)| 免费WEB服务 | 商业WEB服务 | XML Web Servicrs - WEBXML

比如下面这个查手机号归属地的:

 我们新建一个maven工程,在pom文件里引用axis依赖(可以在此连接Apache 轴对axis进行了解)。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yulong.wsdltest</groupId>
    <artifactId>wsdltest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <fastjson.version>1.2.83</fastjson.version>
    </properties>

    <dependencies>
        
        <!--wsdl-->
        <dependency>
            <groupId>axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>com.sixdimensions.commons.osgi.wrapper</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.5</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.0.4</version>
        </dependency>

        <dependency>
            <groupId>axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>apache-log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.8</version>
        </dependency>

        <dependency>
            <groupId>org.lucee</groupId>
            <artifactId>saaj</artifactId>
            <version>1.2.1</version>
        </dependency>

        <dependency>
            <groupId>axis</groupId>
            <artifactId>axis-wsdl4j</artifactId>
            <version>1.5.1</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.2</version>
        </dependency>

    </dependencies>

</project>

然后我们创建一个client。

 

 填入刚刚的wsdl url(http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl)。

 然后就生成了相应的类。

我们再写个测试类调用一下看看。

public static <MobileCodeWSSoap> void main(String[] argv) throws ServiceException, RemoteException {
    MobileCodeWS service = new MobileCodeWSLocator();
    MobileCodeWSSoap_PortType port = service.getMobileCodeWSSoap();
    String body = port.getMobileCodeInfo("15715700000",null);
    System.out.println(body);
  }

发现完美运行:

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值