【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提 ...
随机推荐
- C语言-使用malloc导致的奔溃问题
在使用malloc.memset.free的过程中,出现了程序奔溃,大致现象如下. 程序的实现大致如下: #include <stdio.h> #include <stdlib.h& ...
- webpack处理静态资源
像项目中字体资源是不需要进行打包处理的,可以直接的通过复制方式给打包到目标目录中 # 安装 npm i -D copy-webpack-plugin # 引入 const CopyPlugin = r ...
- 鸿蒙HarmonyOS实战-ArkTS语言基础类库(通知)
前言 移动应用中的通知是指应用程序发送给用户的一种提示或提醒消息.这些通知可以在用户设备的通知中心或状态栏中显示,以提醒用户有关应用程序的活动.事件或重要信息. 移动应用中的通知可以分为两种类型:本地 ...
- 再谈中断机制(APIC)
中断是硬件和软件交互的一种机制,可以说整个操作系统,整个架构都是由中断来驱动的.一个中断的起末会经历设备,中断控制器,CPU 三个阶段:设备产生中断信号,中断控制器翻译信号,CPU 来实际处理信号. ...
- 使用Logstash同步Mysql到Easysearch
从 MySQL 同步数据到 ES 有多种方案,这次我们使用 ELK 技术栈中的 Logstash 来将数据从 MySQL 同步到 Easysearch . 方案前提 MySQL 表记录必须有主键,比如 ...
- C#.NET ASP.NET IIS 加载.pfx私钥证书时报错“出现了内部错误。”
C#.NET ASP.NET IIS 加载.pfx私钥证书时报错"出现了内部错误." 原始代码报错: X509Certificate2 x509cer = new X509Cert ...
- npm 发布自己组件包
npm 发布自己组件包 发布到 npm 上 首先创建自己的npm账号 npm init npm install npm uninstall npm config edit // 编辑 npm conf ...
- idea设置jdk和设置文件编码格式utf-8
1.idea设置jdk 2.idea设置文件编码格式utf-8 create utf-8 files with NO BOM 不要更改,否则编译会出错误.
- 大数据面试吹牛草稿V2.0
面试吹牛之前先打个草稿! 各位面试官好! 我叫 xxx,毕业于 xxx,之前在 xxx 公司待了 1 年多,期间⼀直从事的是 IT 行业,刚开始的时候做的是 Java 开发后来转岗到大数据方向做大数据 ...
- Oracle 三种分页方法
Oracle的三层分页指的是在进行分页查询时,使用三种不同的方式来实现分页效果,分别是使用ROWNUM.使用OFFSET和FETCH.使用ROW_NUMBER() OVER() 1.使用ROWNUM ...