【WSDL】02 四种客户端调用方式
WSDL概念和一些语法内容:
https://www.w3school.com.cn/wsdl/index.asp
SOAP概念:
https://www.runoob.com/soap/soap-tutorial.html
UDDI概念:
https://www.runoob.com/wsdl/wsdl-uddi.html
四种客户端调用方式
公开提供的Web服务列表
http://www.webxml.com.cn/zh_cn/web_services.aspx
一、WSIMPORT自动生成
wsimport.exe 是JDK提供的一个客户端代码生成工具,根据WSDL地址生成
-s 参数 生成Java文件
-d 参数 生成Class文件 (默认生成)
-p 参数 指定包名,声明参数则默认使用命名空间的倒序
局限问题:
wsimport.exe 支持SOAP1.1客户端代码生成
除了入门案例自定义的WSDL,黑马这里还使用了一个WebXml网站的一个WSDL服务
地址:
http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
使用wsimport生成客户端代码:
C:\Users\Administrator\Desktop\temp wsdk>wsimport -s . http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
正在解析 WSDL... [WARNING] 忽略 SOAP 端口 "MobileCodeWSSoap12": 它使用非标准 SOAP 1.2 绑定。
必须指定 "-extension" 选项以使用此绑定。
http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl的第 199 行 [WARNING] 忽略端口 "MobileCodeWSHttpGet": 未指定 SOAP 地址。请尝试运行带 -extension 开关的 wsimport。
http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl的第 202 行 [WARNING] 忽略端口 "MobileCodeWSHttpPost": 未指定 SOAP 地址。请尝试运行带 -extension 开关的 wsimport。
http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl的第 205 行 正在生成代码... 正在编译代码... C:\Users\Administrator\Desktop\temp wsdk>
可以看到SOAP1.2版本协议是wsimport不支持的,所以直接忽略了
删除里面的Class文件和Package-Info文件,放到客户端项目里面

编写客户端启动类:
package cn.cloud9.jax_ws.client; import cn.cloud9.jax_ws.client.intf.mobilecode.MobileCodeWS;
import cn.cloud9.jax_ws.client.intf.mobilecode.MobileCodeWSSoap; /**
* @author OnCloud9
* @description
* @project JAX-WS-Client
* @date 2022年04月23日 14:41
*/
public class MobileClient {
public static void main(String[] args) {
// 服务视图
final MobileCodeWS mobileCodeWS = new MobileCodeWS(); // 获取服务实现类
final MobileCodeWSSoap port = mobileCodeWS.getPort(MobileCodeWSSoap.class); // 调用
final String mobileCodeInfo = port.getMobileCodeInfo("13755645841", null);
System.out.println(mobileCodeInfo);
}
}
启动客户端:
13755645841:江西 南昌 江西移动全球通卡
特点
该种方式使用简单,但一些关键的元素在代码生成时写死到生成代码中,
不方便维护,所以仅用于测试。
二、Service编程
package cn.cloud9.jax_ws.client; import cn.cloud9.jax_ws.client.intf.mobilecode.MobileCodeWSSoap; import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.MalformedURLException;
import java.net.URL; /**
* @author OnCloud9
* @description
* @project JAX-WS-Client
* @date 2022年04月23日 14:41
*/
public class MobileClient {
public static void main(String[] args) throws MalformedURLException {
// WSDL地址
final URL WSDL_LOCATION = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl"); /**
* 创建服务名称
* nameSpaceUri 命名空间地址
* localPart 服务视图名
*/
final QName SERVICE_NAME = new QName("http://WebXml.com.cn/", "MobileCodeWS"); // 服务视图
final Service service = Service.create(WSDL_LOCATION, SERVICE_NAME); // 获取服务实现类, 注意这里,还是需要一个MobileCodeWSSoap.class 接口类
final MobileCodeWSSoap port = service.getPort(MobileCodeWSSoap.class); // 调用
final String mobileCodeInfo = port.getMobileCodeInfo("13184565091", null);
System.out.println(mobileCodeInfo);
}
}
执行结果:
13184565091:江西 南昌 江西联通如意通卡
特点。
该种方式可以自定义关键元素,方便以后维护,是-种标准的开发方式,
三、HttpUrlConnection
开发步骤:。
第一步:创建服务地址。
第二步:打开一个通向服务地址的连接。
第三步:设置参数。
第四步:组织SOAP数据,发送请求+
第五步:接收服务端响应,打印
package cn.cloud9.jax_ws.client; import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; /**
* @author OnCloud9
* @description
* @project JAX-WS-Client
* @date 2022年04月23日 15:03
*/
public class HttpUrlConnectionClient {
public static void main(String[] args) throws Exception {
// 请求服务地址 不需要添加?WSDL参数
URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx");
// 建立链接
final URLConnection urlConnection = url.openConnection();
HttpURLConnection connection = (HttpURLConnection) urlConnection;
// 设置POST请求
connection.setRequestMethod("POST");
// 设置数据格式
connection.setRequestProperty("content-Type", "text/xml;charset=utf-8");
// 设置输入输出权限,connection默认没有
connection.setDoInput(true);
connection.setDoOutput(true); // 设置请求的数据
String payload = getXml("13879105549"); final OutputStream outputStream = connection.getOutputStream();
outputStream.write(payload.getBytes(StandardCharsets.UTF_8)); // 执行发送,获取响应数据
final int responseCode = connection.getResponseCode();
if (responseCode != 200) throw new Exception("请求异常: 响应代码:" + responseCode);
final InputStream inputStream = connection.getInputStream();
final InputStreamReader reader = new InputStreamReader(inputStream);
final BufferedReader bufferedReader = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
String temp = null;
while (null != (temp = bufferedReader.readLine())) sb.append(temp);
System.out.println(sb); bufferedReader.close();
reader.close();
inputStream.close();
} private static String getXml(String phoneNo) {
String[] templates = {
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
"<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/\">\n",
" <soap:Body>\n",
" <getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">\n",
" <mobileCode>", phoneNo, "</mobileCode>\n",
" <userID></userID>\n",
" </getMobileCodeInfo>\n",
" </soap:Body>\n",
"</soap:Envelope>\n"
};
final StringBuffer buffer = new StringBuffer();
Arrays.asList(templates).forEach(buffer::append);
return buffer.toString();
}
}
服务说明和测试见此地址:
http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo
如果请求成功,则会响应SOAP信息
<?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>
<getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
<getMobileCodeInfoResult>13879105549:江西 南昌 江西移动动感地带卡</getMobileCodeInfoResult>
</getMobileCodeInfoResponse>
</soap:Body>
</soap:Envelope>
四、AJAX调用方式
视频演示代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function queryMobileInfo() {
// 创建请求
const xhr = new XMLHttpRequest()
xhr.open('post', 'http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx', true)
xhr.setRequestHeader('content-Type', 'text/xml;charset=utf-8')
xhr.onreadystatechange = function() {
debugger
if (this.readyState !== 4 && this.status !== 200) return
alert(this.responseText)
} // 获取输入的手机号
const phoneNoInput = document.querySelector('#phoneNo')
const phoneNo = phoneNoInput.value let requestSOAP = [
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
"<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/\">\n",
" <soap:Body>\n",
" <getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">\n",
" <mobileCode>", phoneNo, "</mobileCode>\n",
" <userID></userID>\n",
" </getMobileCodeInfo>\n",
" </soap:Body>\n",
"</soap:Envelope>\n"
]
requestSOAP = requestSOAP.join('')
alert(requestSOAP)
xhr.send(requestSOAP)
}
</script>
</head>
<body>
<p>手机号查询<input type="text" id="phoneNo"></p>
<button onclick="javascript:queryMobileInfo()" >点击查询</button> </body>
</html>
但是发现视频用的地址是这个:
http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
我换了直接请求不到了,解析不出来这个地址
然后我看了服务说明:
直接写参数名就够了

但是又给我报错,服务端不给跨域,那就这样了

【WSDL】02 四种客户端调用方式的更多相关文章
- Web Service基础——四种客户端调用方式
通过访问公网服务地址 http://www.webxml.com.cn/zh_cn/index.aspx 来演示四种不同的客户端调用方式 1. 生成客户端调用方式 1.1 Wsimport命令介绍 首 ...
- ASP.NET MVC下的四种验证编程方式[续篇]
在<ASP.NET MVC下的四种验证编程方式>一文中我们介绍了ASP.NET MVC支持的四种服务端验证的编程方式("手工验证"."标注Validation ...
- ASP.NET MVC下的四种验证编程方式
ASP.NET MVC采用Model绑定为目标Action生成了相应的参数列表,但是在真正执行目标Action方法之前,还需要对绑定的参数实施验证以确保其有效性,我们将针对参数的验证成为Model绑定 ...
- ASP.NET MVC下的四种验证编程方式【转】
ASP.NET MVC采用Model绑定为目标Action生成了相应的参数列表,但是在真正执行目标Action方法之前,还需要对绑定的参数实施验证以确保其有效 性,我们将针对参数的验证成为Model绑 ...
- thinkphp四种url访问方式详解
本文实例分析了thinkphp的四种url访问方式.分享给大家供大家参考.具体分析如下: 一.什么是MVC thinkphp的MVC模式非常灵活,即使只有三个中和一个也可以运行. M -Model 编 ...
- ASP.NET MVC下的四种验证编程方式[续篇]【转】
在<ASP.NET MVC下的四种验证编程方式> 一文中我们介绍了ASP.NET MVC支持的四种服务端验证的编程方式(“手工验证”.“标注ValidationAttribute特性”.“ ...
- python接口自动化(十)--post请求四种传送正文方式(详解)
简介 post请求我在python接口自动化(八)--发送post请求的接口(详解)已经讲过一部分了,主要是发送一些较长的数据,还有就是数据比较安全等.我们要知道post请求四种传送正文方式首先需要先 ...
- python3+requests:post请求四种传送正文方式(详解)
前言:post请求我在python接口自动化2-发送post请求详解(二)已经讲过一部分了,主要是发送一些较长的数据,还有就是数据比较安全等,可以参考Get,Post请求方式经典详解进行学习一下. 我 ...
- python3+requests:post请求四种传送正文方式
https://www.cnblogs.com/insane-Mr-Li/p/9145152.html 前言:post请求我在python接口自动化2-发送post请求详解(二)已经讲过一部分了,主要 ...
- spring四种依赖注入方式(转)
spring四种依赖注入方式!! 平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提 ...
随机推荐
- 解决 Https 站点请求 Http 接口服务后报 the content must be served over HTTPS 错误的问题
问题分析 之前将自己所有的 Http 站点全部更新为 Https 站点,但是在请求后台接口服务的时候还是 Http 请求,导致部署之后,直接在控制台报 This request has been bl ...
- 算法金 | 读者问了个关于深度学习卷积神经网络(CNN)核心概念的问题
大侠幸会,在下全网同名[算法金] 0 基础转 AI 上岸,多个算法赛 Top [日更万日,让更多人享受智能乐趣] 读者问了个关于卷积神经网络核心概念的问题,如下, [问]神经元.权重.激活函数.参数 ...
- Symbol.for()
当我们在不同的模块或文件中需要共享一个特定的Symbol时,可以使用Symbol.for()方法来实现. 假设我们有两个模块,分别是module1.js和module2.js.我们希望在这两个模块中使 ...
- 获取URL中查询参数 URL中动态参数
通过 req.query 对象,可以访问到客户端通过查询字符串的形式发送到服务器的参数 app.get('/',(req,res)=>{ console.log(req.query) }) .U ...
- 关于使用Gitlab CI-CD
关于使用 Gitlab CI/CD 如果是个人建议自己写脚本,手动运行,而不是使用 Gitlab CI/CD. 免费的 Runner 需要 Credit Card!
- CF1838A-Blackboard-List
题意简述 在黑板上有两个数字,进行如下操作 \(n-2\) 次: 每次在黑板上选择任意两个数,将两个数的差的绝对值写在黑板上. 这样你会得到一个长度为 \(n (3 \le n \le 100)\) ...
- sqlite3自动插入创建时间和更新时间
最近在记录一些简单的结构化日志信息时,用到了sqlite3数据库(保存的信息比较简单,用Mysql,SQL Server,Postgres这些数据库有点小题大做). 以前开发系统时,用Mysql和Po ...
- C# WINFORM 获取上级目录
MessageBox.Show(Application.StartupPath); DirectoryInfo di = new DirectoryInfo(string.Format(@" ...
- re.search()用法详解
re.search() 是 Python 的正则表达式库 re 中的一个方法,用于在字符串中搜索与正则表达式模式匹配的第一个位置,并返回一个匹配对象.如果没有找到匹配项,则返回 None. 以下是 r ...
- QMS质量管理系统:打造企业质量控制的新纪元
在当今竞争激烈的市场环境下,产品质量是决定企业生存与发展的关键因素之一.为了确保从设计到交付的每一步都符合最高标准,一套高效.全面的质量管理系统(Quality Management System, ...