English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Simple Object Access Protocol (SOAP, full name Simple Object Access Protocol) is a protocol specification for data exchange.
SOAP is a simple XML-based protocol that allows applications to exchange information via HTTP.
The Simple Object Access Protocol (SOAP) is a protocol specification for data exchange, which is a lightweight, simple protocol based on XML (a subset of the Standard Generalized Markup Language). It is designed to exchange structured and fixed information on the WEB.
SOAP4R is developed and implemented by Hiroshi Nakamura, used for Ruby SOAP applications.
SOAP4R download address:http://raa.ruby-lang.org/project/soap4r/.
Note:Your Ruby environment may have already installed this component.
Under the Linux environment, you can also use gem to install this component, the command is as follows:
gem install soap4r --include-dependencies
If you are developing on a Windows environment, you need to download the zip compressed file and install it by executing install.rb.
SOAP4R supports two different types of services:
Based on CGI/FastCGI service (SOAP::RPC::CGIStub)
Independent service (SOAP::RPC:StandaloneServer)
This tutorial will introduce how to establish an independent SOAP service. The steps are as follows:
To implement your own independent server, you need to write a new class that is a subclass of SOAP::RPC::StandaloneServer:
class MyServer < SOAP::RPC::StandaloneServer ............. end
Note:If you want to write a server based on FastCGI, you need to inherit the SOAP::RPC::CGIStub class, and the rest of the program will remain unchanged.
Next, we define the methods of the Web Service, as follows, we define two methods, one for adding two numbers and one for dividing two numbers:
class MyServer < SOAP::RPC::StandaloneServer ............. # Handling method def add(a, b) return a + b end def div(a, b) return a / b end end
Next, add the methods we define on the server, the initialize method is public and used for external connections:
class MyServer < SOAP::RPC::StandaloneServer def initialize(*args) add_method(receiver, inoutParam, *paramArg) end end
The following is the description of each parameter:
Parameters | Description |
---|---|
receiver | The object containing the method name. If you define the service method in the same class, this parameter is self. |
methodName | the method name of the RPC request called. |
paramArg | parameter names and parameter modes |
To understand inout and out The parameters, consider the following service method, which requires two parameters: inParam and inoutParam, the function returns three values after execution: retVal, inoutParam, and outParam:
def aMeth(inParam, inoutParam) retVal = inParam + inoutParam outParam = inParam . inoutParam inoutParam = inParam * inoutParam return retVal, inoutParam, outParam end
The following are the public calling methods:
add_method(self, 'aMeth', [ %w(in inParam), %w(inout inoutParam), %w(out outParam), %w(retval return) ])
Finally, we instantiate the derived class and call the start method to start the service:
myServer = MyServer.new('ServerName', 'urn:ruby:ServiceName', hostname, port) myServer.start
The following is the description of the request parameters:
Parameters | Description |
---|---|
ServerName | service name, you can choose your favorite |
urn:ruby:ServiceName | Here urn:ruby is fixed, but you can give your service a unique ServiceName |
hostname | Specify the hostname |
port | Web service port |
Next, we create an independent service through the above steps:
require "soap/rpc/standaloneserver" begin class MyServer < SOAP::RPC::StandaloneServer # Expose our service def initialize(*args) add_method(self, 'add', 'a', 'b') add_method(self, 'div', 'a', 'b') end # Handler methods def add(a, b) return a + b end def div(a, b) return a / b end end server = MyServer.new("MyServer", 'urn:ruby:calculation', 'localhost', 8080) trap('INT') { server.shutdown } server.start rescue => err puts err.message end
After executing the above program, a listener is started 808Local service of port 0, and publicly exposes two methods: add and div.
You can run the above service in the background:
$ ruby MyServer.rb &
Develop SOAP clients using the SOAP::RPC::Driver class in Ruby. Let's take a detailed look at the usage of the SOAP::RPC::Driver class.
The following information is required to call the SOAP service:
SOAP service URL address (SOAP Endpoint URL)
Service method namespace (Method Namespace URI)
Service method name and parameter information
Next, we will step by step create a SOAP client to call the above SOAP methods: add, div:
We can call its new method by instantiating the SOAP::RPC::Driver class, as shown below:
SOAP::RPC::Driver.new(endPoint, nameSpace, soapAction)
The following is the description of the parameters:
Parameters | Description |
---|---|
endPoint | The URL address of the connection to the SOAP service |
nameSpace | Namespace for all RPCs of the SOAP::RPC::Driver object. |
soapAction | The value of the SOAPAction field in the HTTP header. If it is a string and it is "", then the default is nil |
To add a SOAP service method for SOAP::RPC::Driver, we can call the following method through the example SOAP::RPC::Driver:
driver.add_method(name, *paramArg)
The following is the description of the parameters:
Parameters | Description |
---|---|
name | The method name of the remote web service |
paramArg | Specify the parameters of the remote procedure |
Finally, we can use the SOAP::RPC::Driver example to call the SOAP service:
result = driver.serviceMethod(paramArg...)
serviceMethod is the actual method name of the SOAP service, and paramArg is the list of method parameters.
Based on the above steps, we can write the following SOAP client:
#!/usr/bin/ruby -w require 'soap'/rpc/driver' NAMESPACE = 'urn:ruby:calculation' URL = 'http://localhost:8080/' begin driver = SOAP::RPC::Driver.new(URL, NAMESPACE) # Add remote service methods driver.add_method('add', 'a', 'b') # Call remote service methods puts driver.add(20, 30) rescue => err puts err.message end
In this section, we just briefly introduce Ruby's Web Service. If you want to learn more, you can view the official documentation:Ruby's Web Service