微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用多种方式调用webservice服务

客户端调用webService有多种方式:

下面就以为http://www.webxml.com.cn/zh_cn/index.aspx上提供的,查询手机号码为例,阐述一下几种方式:

1: 使用wsimport,或者是第三方框架的命令(比如cxf的wsdl2java)来自动生成代码

首先,在命令行中运行:

wsimport -s . http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
生成客户端代码,并把生成的源码拷贝到项目中.


然后,调用

public class PhoneCodeApp {

	public static void main(String[] args) {
		MobileCodeWS server = new MobileCodeWS();
		MobileCodeWSSoap soapPortType = server.getMobileCodeWSSoap();
		arrayofstring result = soapPortType.getDatabaseInfo();
		for (String str : result.getString()) {
			System.out.println(str);
		}

		System.out.println(soapPortType.getMobileCodeInfo("15513061132",""));
	}
}

2: 利用jax-ws中的service类,收到编写代码进行调用

首先,也是需要命令来获得客户端代码,但是仅仅需要soap调用接口.


然后编写代码:

public class ServiceCode {
	public static void main(String[] args) throws Exception {

		URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
		QName qName = new QName("http://WebXml.com.cn/","MobileCodeWS");
		Service service = Service.create(url,qName);

		MobileCodeWSSoap portType = service.getPort(MobileCodeWSSoap.class);

		arrayofstring string = portType.getDatabaseInfo();
		for (String str : string.getString()) {
			System.out.println(str);
		}

		System.out.println(portType.getMobileCodeInfo("15513061132",""));
	}
}
3: 使用urlconnection手动组装soap请求协议来调用服务
这种方式与上面2种不同,是通过组装请求协议来调用的,因此不需要运行命令来获得代码

只需要编写代码即可.

public class PhoneCodeApp {

	public static void main(String[] args) throws Exception {

		// 建立连接
		URL url = new URL(
				"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setDoInput(true);
		connection.setDoOutput(true);
		connection.setRequestMethod("POST");
		connection.setRequestProperty("Content-Type","text/xml;charset=utf-8");
		connection.setRequestProperty("SOAPAction","http://WebXml.com.cn/getDatabaseInfo");

		// 发送数据
		OutputStream outputStream = connection.getoutputStream();
		String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><getDatabaseInfo xmlns=\"http://WebXml.com.cn/\" /></soap:Body></soap:Envelope>";
		outputStream.write(str.getBytes(Charset.forName("utf-8")));

		connection.connect();

		// 接受数据
		InputStream inputStream = connection.getInputStream();

		outputStream.close();
		inputStream.close();
		connection.disconnect();

		// 解析文档
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse(inputStream);
		NodeList nodeList = document.getElementsByTagName("string");
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i);
			System.out.println(node.getTextContent());
		}

	}

}

4: 在页面上通过ajax来调用

这种方式本质同上面的方式一样,直接上代码

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	window.onload = function() {
		document.getElementById("btn").onclick = function() {
			var xmlHttpRequest = null;
			try {
				xmlHttpRequest = new XMLHttpRequest();	
			} catch (e) {
				try {
					xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
				}
			}			
			
			var reqeustStr = '<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><getDatabaseInfo xmlns=\"http://WebXml.com.cn/\" /></soap:Body></soap:Envelope>';
			xmlHttpRequest.open("POST","http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx",true);
			
			xmlHttpRequest.setRequestHeader("Content-Type","text/xml;charset=utf-8");
			
			xmlHttpRequest.send(reqeustStr);
			
			xmlHttpRequest.onreadystatechange = function() {
				if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
					alert(xmlHttpRequest.responseText);					
				}
			}
		}
	}
</script>
</head>
<body>
	<input type="button" value="发送请求" id="btn"/>
</body>
</html>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐