Calling a SOAP Web Service from the Command Line – 从命令行调用一个SOAP网络服务

最后修改: 2021年 7月 29日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’re going to show how we can use different command-line interface (CLI) processes to consume a SOAP web service.

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

2. SOAP Web Service

2.SOAP网络服务

To run the examples in this post, we’ll use the SOAP web service developed in a previous article. In a nutshell, this service has an endpoint that clients can access, providing a country name in the request. The service replies with the name of the country’s capital, population, and currency.

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

3. cURL

3.cURL

Let’s start with cURL because it’s probably the most widely used command-line tool for transferring data via network protocols. To test a SOAP web service, we just need to make HTTP requests with a SOAP envelope in the request body.

让我们从cURL开始,因为它可能是最广泛使用的通过网络协议传输数据的命令行工具。要测试一个SOAP网络服务,我们只需要发出HTTP请求,请求体中有一个SOAP信封

For our web service, a simple HTTP POST request is:

对于我们的网络服务,一个简单的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

We don’t need to specify that we’re using HTTP because it’s the default protocol in cURL. Since we’re testing the request, we use the verbose mode via the -v option.

我们不需要指定使用HTTP,因为它是cURL的默认协议。由于我们是在测试请求,所以我们通过-v选项使用了verbose模式。

Inside the SOAP envelope, we specify the country (Poland) and finish the command with the SOAP server URL. We have installed the server locally on our computer, using the example from our earlier article.

在SOAP信封中,我们指定了国家(波兰),并用SOAP服务器的URL来完成命令。我们已经在我们的计算机上安装了服务器,使用了先前文章中的例子

Since we’re using the -v option, we get a detailed response:

由于我们使用的是-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

The request and response messages for SOAP web services can be long, so it’s more convenient to store them in files. If we save the request body in request.xml and redirect the output of the response to the file response.xml, the command, in this case, is very simple:

SOAP网络服务的请求和响应信息可能很长,所以把它们存储在文件中更方便。如果我们将请求主体保存在request.xml中,并将响应的输出重定向到response.xml文件中,在这种情况下,命令就非常简单了

curl --header "Content-Type: text/xml;charset=UTF-8" -d @request.xml -o response.xml http://localhost:8080/ws

In general, it’s not necessary to specify POST in the command as we did before because it’s inferred by cURL.

一般来说,我们没有必要像以前那样在命令中指定POST,因为它是由cURL推断出来的。

If we need to read the response in the terminal, it’s best to pipe the command with xmllint to get the XML response properly formatted:

如果我们需要在终端读取响应,最好是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

4.Wget

Let’s make the same request using Wget:

让我们用Wget来做同样的请求。

wget --post-file=request.xml --header="Content-Type: text/xml" http://localhost:8080/ws -O response.xml

The response is:

答复是。

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’

The syntax is similar to cURL, and we can have the request and response bodies stored in files as before.

语法与cURL相似,我们可以像以前一样将请求和响应体存储在文件中。

5. HTTPie

5.ǞǞǞ

Wget and cURL are very useful commands to quickly test a SOAP server. They’re available with all major OS distributions. They can also be easily integrated with shell scripts.

Wget和cURL是快速测试SOAP服务器的非常有用的命令。它们在所有主要的操作系统发行版中都可用。它们也可以很容易地与shell脚本集成。

The advantage of HTTPie is that it provides a very intuitive way to interact with Web Services. As stated in the documentation: “HTTPie is designed for testing, debugging, and generally interacting with APIs & HTTP servers. They use simple and natural syntax and provide formatted and colorized output.”

HTTPie的优势在于它提供了一种非常直观的方式来与Web服务进行交互。正如文档中所述:”HTTPie是为测试、调试和一般与API及HTTP服务器互动而设计的。它们使用简单自然的语法,并提供格式化和彩色化的输出”。

Let’s issue the simple request we did before, this time using HTTPie:

让我们发出我们之前做的简单请求,这次使用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'

If we want to extract the request body from a file:

如果我们想从一个文件中提取请求正文。

http -b POST http://localhost:8080/ws 'Content-Type:text/xml' < request.xml

It’s as simple as using file input redirection.

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

For more details on how to use HTTPie, refer to the documentation.

关于如何使用HTTPie的更多细节,请参考文档。

6. Summary

6.归纳总结

We’ve seen simple examples of how to quickly call a SOAP Web Service from the command line using cURL, Wget, and HTTPie. We also have done a brief comparison of the three tools and when to use them.

我们已经看到了如何使用cURL、Wget和HTTPie从命令行快速调用SOAP网络服务的简单例子。我们还对这三种工具做了一个简单的比较,以及何时使用它们。