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 四种客户端调用方式的更多相关文章

  1. Web Service基础——四种客户端调用方式

    通过访问公网服务地址 http://www.webxml.com.cn/zh_cn/index.aspx 来演示四种不同的客户端调用方式 1. 生成客户端调用方式 1.1 Wsimport命令介绍 首 ...

  2. ASP.NET MVC下的四种验证编程方式[续篇]

    在<ASP.NET MVC下的四种验证编程方式>一文中我们介绍了ASP.NET MVC支持的四种服务端验证的编程方式("手工验证"."标注Validation ...

  3. ASP.NET MVC下的四种验证编程方式

    ASP.NET MVC采用Model绑定为目标Action生成了相应的参数列表,但是在真正执行目标Action方法之前,还需要对绑定的参数实施验证以确保其有效性,我们将针对参数的验证成为Model绑定 ...

  4. ASP.NET MVC下的四种验证编程方式【转】

    ASP.NET MVC采用Model绑定为目标Action生成了相应的参数列表,但是在真正执行目标Action方法之前,还需要对绑定的参数实施验证以确保其有效 性,我们将针对参数的验证成为Model绑 ...

  5. thinkphp四种url访问方式详解

    本文实例分析了thinkphp的四种url访问方式.分享给大家供大家参考.具体分析如下: 一.什么是MVC thinkphp的MVC模式非常灵活,即使只有三个中和一个也可以运行. M -Model 编 ...

  6. ASP.NET MVC下的四种验证编程方式[续篇]【转】

    在<ASP.NET MVC下的四种验证编程方式> 一文中我们介绍了ASP.NET MVC支持的四种服务端验证的编程方式(“手工验证”.“标注ValidationAttribute特性”.“ ...

  7. python接口自动化(十)--post请求四种传送正文方式(详解)

    简介 post请求我在python接口自动化(八)--发送post请求的接口(详解)已经讲过一部分了,主要是发送一些较长的数据,还有就是数据比较安全等.我们要知道post请求四种传送正文方式首先需要先 ...

  8. python3+requests:post请求四种传送正文方式(详解)

    前言:post请求我在python接口自动化2-发送post请求详解(二)已经讲过一部分了,主要是发送一些较长的数据,还有就是数据比较安全等,可以参考Get,Post请求方式经典详解进行学习一下. 我 ...

  9. python3+requests:post请求四种传送正文方式

    https://www.cnblogs.com/insane-Mr-Li/p/9145152.html 前言:post请求我在python接口自动化2-发送post请求详解(二)已经讲过一部分了,主要 ...

  10. spring四种依赖注入方式(转)

    spring四种依赖注入方式!! 平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提 ...

随机推荐

  1. SCOI 回旋退役记

    02.21 day -2 开始写了,期望这不是真的退役记吧.但是不是的概率好小-- 这几天一直考试,怎么说呢,到差不差的,也就那个样子. 归根结底,菜是原罪,和那些大佬相比我真的很很很菜啊.当时看 c ...

  2. golang sync.Once 保证某个动作仅执行一次的机制

    type Once struct { done atomic.Uint32 m Mutex } 这段代码是 Go 语言标准库中 sync 包的一部分,定义了一个 Once 类型.Once 类型用于确保 ...

  3. CPU的一、二、三级缓存的区别

    引言 概念 缓存大小也是CPU的重要指标之一,而且缓存的结构和大小对CPU速度的影响非常大,CPU内缓存的运行频率极高,一般是和处理器同频 运作,工作效率远远大于系统内存和硬盘.实际工作时,CPU往往 ...

  4. python 文件查找及截取字符串 (替换,分割) demo

    #"F:\\test.txt" ''' # 例1:字符串截取 str = '12345678' print str[0:1] # 例2:字符串替换 str = 'akakak' s ...

  5. new 和 delete 运算符

    C++ 支持使用操作符 new 和 delete 来动态分配和释放对象. new 运算符调用特殊函数 operator new,delete 运算符调用特殊函数 operator delete. 如果 ...

  6. Python 潮流周刊#57:Python 该采用日历版本吗?

    本周刊由 Python猫 出品,精心筛选国内外的 250+ 信息源,为你挑选最值得分享的文章.教程.开源项目.软件工具.播客和视频.热门话题等内容.愿景:帮助所有读者精进 Python 技术,并增长职 ...

  7. markdown折叠展开代码

    背景 有的时候,我们的代码太多,直接用cout<<"hello";很不方便. 我们可以将代码折叠. 效果 代码 普通代码折叠 <details> <s ...

  8. 【动手学深度学习】第五章笔记:层与块、参数管理、自定义层、读写文件、GPU

    为了更好的阅读体验,请点击这里 由于本章内容比较少且以后很显然会经常回来翻,因此会写得比较详细. 5.1 层和块 事实证明,研究讨论"比单个层大"但"比整个模型小&quo ...

  9. [FLET] 02 route 测试

    from typing import Dict import flet from flet import AppBar, ElevatedButton, Page, Text, View, color ...

  10. 【ClickHouse】4:clickhouse基本操作二 建库建表导数据

    背景介绍: 有三台CentOS7服务器安装了ClickHouse HostName IP 安装程序 程序端口 centf8118.sharding1.db 192.168.81.18 clickhou ...