1. 概述

在本教程中,我们将展示如何使用不同的命令行界面 (CLI) 进程来使用 SOAP Web 服务。

2.SOAP网络服务

为了运行本文中的示例,我们将使用上一篇文章中开发的 SOAP Web 服务。简而言之,该服务有一个客户端可以访问的端点,并在请求中提供国家/地区名称。该服务会回复该国家的首都名称、人口和货币。

3. 卷曲

让我们从 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.baeldung.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.baeldung.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.baeldung.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 --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.baeldung.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

就像使用文件输入重定向一样简单。

有关如何使用 HTTPie 的更多详细信息,请参阅文档。

六、总结

我们已经了解了如何使用 cURL、Wget 和 HTTPie 从命令行快速调用 SOAP Web 服务的简单示例。我们还对这三种工具以及何时使用它们进行了简要比较。