从命令行调用SOAP Web服务
1. 概述
在本教程中,我们将展示如何使用不同的命令行界面 (CLI) 进程来使用 SOAP Web 服务。
2. SOAP 网络服务
为了运行本文中的示例,我们将使用在前一篇文章 中开发的 SOAP Web 服务。简而言之,该服务有一个客户端可以访问的端点,在请求中提供一个国家名称。该服务以该国首都、人口和货币的名称进行回复。
3. SOAP
让我们从 cURL 开始,因为它可能是通过网络协议传输数据的最广泛使用的命令行工具。要测试 SOAP Web 服务,我们只需要在请求正文中使用 SOAP 信封发出 HTTP 请求。
对于我们的 Web 服务,一个简单的 HTTP POST 请求是:
curl -v --request POST --header "Content-Type: text/xml;charset=UTF-8" \
--data \
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:gs="http://www.blogdemo.com/springsoap/gen"> \
<soapenv:Header/> \
<soapenv:Body> \
<gs:getCountryRequest> <gs:name>Poland</gs:name> </gs:getCountryRequest> \
</soapenv:Body> \
</soapenv:Envelope>' \
http://localhost:8080/ws
我们不需要指定我们正在使用 HTTP,因为它是 cURL 中的默认协议。由于我们正在测试请求,我们通过*-v*选项使用详细模式。
在 SOAP 信封内,我们指定国家(波兰)并使用 SOAP 服务器 URL 完成命令。我们已经在我们的计算机上本地安装了服务器,使用我们之前文章 中的示例。
由于我们使用了*-v*选项,我们得到了详细的响应:
* Connected to localhost (::1) port 8080 (#0)
> POST /ws HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.55.1
> Accept: */*
> Content-Type: text/xml;charset=UTF-8
> Content-Length: 282
>
* upload completely sent off: 282 out of 282 bytes
< HTTP/1.1 200
< Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
< SOAPAction: ""
< Content-Type: text/xml;charset=utf-8
< Content-Length: 407
< Date: Sun, 18 Jul 2021 23:46:38 GMT
<
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns2:getCountryResponse xmlns:ns
2="http://www.blogdemo.com/springsoap/gen"><ns2:country><ns2:name>Poland</ns2:name><ns2:population>38186860</ns2:population><ns2:capital>Warsaw
</ns2:capital><ns2:currency>PLN</ns2:currency></ns2:country></ns2:getCountryResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>* Connection #0 to hos
t localhost left intact
SOAP Web 服务的请求和响应消息可能很长,因此将它们存储在文件中更方便。如果我们将请求正文保存在request.xml中并将响应的输出重定向到文件response.xml,在这种情况下,命令非常简单:
curl --header "Content-Type: text/xml;charset=UTF-8" -d @request.xml -o response.xml http://localhost:8080/ws
一般来说,不需要像我们之前那样在命令中指定 POST,因为它是由 cURL 推断的。
如果我们需要在终端中读取响应,最好使用xmllint管道命令以正确格式化 XML 响应:
curl --request POST --header "Content-Type: text/xml;charset=UTF-8" -d @request.xml http://localhost:8080/ws | xmllint --format -
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 725 100 407 100 318 407 318 0:00:01 --:--:-- 0:00:01 15425<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns2:getCountryResponse xmlns:ns2="http://www.blogdemo.com/springsoap/gen">
<ns2:country>
<ns2:name>Poland</ns2:name>
<ns2:population>38186860</ns2:population>
<ns2:capital>Warsaw</ns2:capital>
<ns2:currency>PLN</ns2:currency>
</ns2:country>
</ns2:getCountryResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
4. wget
让我们使用 Wget 发出相同的请求:
wget --post-file=request.xml --header="Content-Type: text/xml" http://localhost:8080/ws -O response.xml
回应是:
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:8080... connected.
HTTP request sent, awaiting response... 200
Length: 407 [text/xml]
Saving to: ‘response.xml’
语法类似于 cURL,我们可以像以前一样将请求和响应主体存储在文件中。
5. HTTPie
Wget 和 cURL 是用于快速测试 SOAP 服务器的非常有用的命令。它们适用于所有主要的操作系统发行版。它们也可以很容易地与 shell 脚本集成。
HTTPie 的优势在于它提供了一种与 Web 服务交互的非常直观的方式。如文档 中所述:“HTTPie 设计用于测试、调试以及通常与 API 和 HTTP 服务器交互。它们使用简单自然的语法,并提供格式化和彩色输出。”
让我们发出我们之前做的简单请求,这次使用 HTTPie:
echo '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:gs="http://www.blogdemo.com/springsoap/gen"> \
<soapenv:Header/> \
<soapenv:Body> \
<gs:getCountryRequest> <gs:name>Poland</gs:name> </gs:getCountryRequest> \
</soapenv:Body> \
</soapenv:Envelope>' | \
http -b POST http://localhost:8080/ws 'Content-Type:text/xml'
如果我们想从文件中提取请求正文:
http -b POST http://localhost:8080/ws 'Content-Type:text/xml' < request.xml
它就像使用文件输入重定向一样简单。