Java两种自用的请求WebService接口的方法

一、前言

我将以查询国内手机号码归属地省份、地区和手机卡类型信息这个样例来进行测试,接口管理地址http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx

1.1 什么是SOAP?

soap用来描述传递信息的格式, WSDL 用来描述如何访问具体的接口, uddi用来管理,分发,查询webService 。

1.2 SOAP的优点

对于应用程序开发来说,使程序之间进行因特网通信是很重要的。目前的应用程序通过使用远程过程调用(RPC)在诸如 DCOM 与 CORBA 等对象之间进行通信,但是 HTTP 不是为此设计的。RPC 会产生兼容性以及安全问题;防火墙和代理服务器通常会阻止此类流量。通过 HTTP 在应用程序间通信是更好的方法,因为 HTTP 得到了所有的因特网浏览器及服务器的支持。SOAP 就是被创造出来完成这个任务的。SOAP 提供了一种标准的方法,使得运行在不同的操作系统并使用不同的技术和编程语言的应用程序可以互相进行通信。

1.3 什么是WebService

WebService是一种跨编程语言、跨操作系统平台的远程调用技术。

二、调用webservice的两种方法

2.1 方法一:wsimport命令生成客户端

2.1.1 wsimport生成代码

wsimport命令样式

wsimport -encoding utf-8 -keep -s D:\test -p com.lawyer.user -verbose http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

-encoding : 指定编码格式

-keep:是否生成java源文件

-d:指定.class文件的输出目录

-s:指定.java文件的输出目录

-p:定义生成类的包名,不定义的话有默认包名

-verbose:在控制台显示输出信息

-b:指定jaxws/jaxb绑定文件或额外的schemas

-extension:使用扩展来支持SOAP1.2

生成代码图

2.1.2 将生成的代码copy至项目

复制代码进项目

2.1.3 测试

测试代码

@Test
	public void getMobileInfo(){
		MobileCodeWS mobileCodeWS = new MobileCodeWS();
		MobileCodeWSSoap soap = mobileCodeWS.getMobileCodeWSSoap();
		String result = soap.getMobileCodeInfo("18692244923", "");
		System.out.println("输出结果为:"+result);
	}

输出结果

输出结果为:18692244923:湖南 长沙 湖南联通GSM卡

2.2 方法二:通过apache的axis调用远程的web service

2.2.1 导入jar包
	<!--webService调用-->
		<!-- https://mvnrepository.com/artifact/javax.activation/javax.activation-api -->
		<dependency>
			<groupId>javax.activation</groupId>
			<artifactId>javax.activation-api</artifactId>
			<version>1.2.0</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/axis/axis -->
		<dependency>
			<groupId>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/commons-httpclient/commons-httpclient -->
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
		<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
		<dependency>
			<groupId>javax.mail</groupId>
			<artifactId>mail</artifactId>
			<version>1.4.7</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/wsdl4j/wsdl4j -->
		<dependency>
			<groupId>wsdl4j</groupId>
			<artifactId>wsdl4j</artifactId>
			<version>1.6.3</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
		<dependency>
			<groupId>dom4j</groupId>
			<artifactId>dom4j</artifactId>
			<version>1.6.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.axiom/com.springsource.org.apache.axiom -->
		<dependency>
			<groupId>org.apache.axiom</groupId>
			<artifactId>com.springsource.org.apache.axiom</artifactId>
			<version>1.2.13</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.apache.axis2/axis2-adb -->
		<dependency>
			<groupId>org.apache.axis2</groupId>
			<artifactId>axis2-adb</artifactId>
			<version>1.7.9</version>
		</dependency>
2.2.2 测试代码
@Test
	public void axis(){
		//接口库地址
		String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx";
		//xml命名空间
		String soap = "http://WebXml.com.cn/";
		//定义axis客户端服务
		Service service = new Service();
		//定义接口访问参数
		String mobileCode = "18692244923";
		String userID = "";

		try {
			//设置axis访问客户端参数
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(url);
			call.setOperationName(new QName(soap,"getMobileCodeInfo"));
			//设置参数
			call.addParameter(new QName(soap,"mobileCode"), XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter(new QName(soap,"userID"), XMLType.XSD_STRING, ParameterMode.IN);
			//返回类型设置
			call.setReturnType(XMLType.XSD_STRING);

			call.setUseSOAPAction(true);
			call.setSOAPActionURI(soap+"getMobileCodeInfo");
			//发起请求,携带前定义的参数值
			String result = (String) call.invoke(new Object[]{mobileCode,userID});
			System.out.println("输出结果为:"+result);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

	}

输出结果

输出结果为:18692244923:湖南 长沙 湖南联通GSM卡
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值