客户端调用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>