Как использовать SOAP без Ruby - PullRequest
0 голосов
/ 27 января 2020

Я использую Savon, и он работает. Мой WSDL:

<definitions name="demo" targetNamespace="http://localhost:8090/demo">
<message name="DemoRequest">
<part name="param1" type="xsd:string"/>
</message>
<message name="DemoResponse">
<part name="Result" type="xsd:string"/>
</message>
<portType name="DemoPortType">
<operation name="Demo">
<input message="tns:DemoRequest"/>  
<output message="tns:DemoResponse"/>
</operation>
</portType>
<binding name="DemoBinding" type="tns:DemoPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="Demo">
<soap:operation soapAction="http://localhost:8090/Demo"/>
<input>
<soap:body use="encoded" namespace="http://localhost:8090/demo" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded" namespace="http://localhost:8090/demo"   encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
</binding>
<service name="DemoService">
<port name="DemoPort" binding="DemoBinding">
<soap:address location="http://localhost:8090/service.php"/>
</port>
</service>
</definitions>

Моя Ruby программа:

client=Savon.client(wsdl: "http://localhost:8090/demo.wsdl")
client.operations
=> [:demo]
client.call(:demo,message: {param1:"hola"}).body
=> {:demo_response=>{:result=>"Request received with param1 = hola"}}

Могу ли я использовать это без Savon, что-то вроде этого? переменная данных скопирована из справочника, в котором я использую Pro PHP Тестирование фреймворков Patterns и страницы со списком More 19-2 * 287

require 'net/https'
require 'uri'
require 'nokogiri'
url = "http://localhost:8090/demo.wsdl"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = false

# Raw SOAP XML to be passed
# TODO : Date should be dynamic
data = <<-EOF
<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>
    <ns1:Demo>
    <param1 xsi:type="xsd:string">abcdefg</param1>
</ns1:Demo>
  </soap:Body>
</soap:Envelope>
EOF

# # Set Headers
# SOAPAction is mandatory
headers = {'Content-Type' => 'text/xml; charset=utf-8','SOAPAction' => "http://localhost:8090/Demo"}

result= http.post(uri.path, data, headers)

# Parse
doc = Nokogiri::XML(result.body)
doc.remove_namespaces! # Remove namespaces from xml to make it clean
p doc

Результат переменной result выглядит примерно так:

{"date":["Mon, 27 Jan 2020 16:51:03 GMT"],"server":["Apache/2.4.41 (Unix) OpenSSL/1.1.1d PHP/7.4.1 mod_perl/2.0.8-dev Perl/v5.16.3"],"last-modified":["Sun, 26 Jan 2020 21:17:35 GMT"],"etag":["\"57c-59d118504f8aa\""],"accept-ranges":["bytes"],"content-length":["1404"],"connection":["close"]} 

и переменная result.body - это тот же файл wsdl, но без ответа

<?xml version ="1.0" encoding ="UTF-8" ?>
<definitions name="demo"
targetNamespace="http://localhost:8090/demo"
xmlns:tns="http://localhost:8090/demo"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/">
<message name="DemoRequest">
<part name="param1" type="xsd:string" />
</message>
<message name="DemoResponse">
<part name="Result" type="xsd:string" />
</message>
<portType name="DemoPortType">
<operation name="Demo">
<input message="tns:DemoRequest" />
<output message="tns:DemoResponse" />
</operation>
</portType>
<binding name="DemoBinding" type="tns:DemoPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="Demo">
<soap:operation soapAction="http://localhost:8090/Demo" />
<input>
<soap:body use="encoded" namespace="http://localhost:8090/demo"    encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body use="encoded" namespace="http://localhost:8090/demo"    encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
</binding>
<service name="DemoService">
<port name="DemoPort" binding="DemoBinding">
<soap:address location="http://localhost:8090/service.php" />
</port>
</service>
</definitions>

Я пытался увидеть что-то вроде Savon:

Request received with param1 = abcdefg"

, но это не сработало.

1 Ответ

0 голосов
/ 28 января 2020

Похоже, ваше сообщение недействительно.

Попробуйте отправить это

data = %(
  <soapenv:Envelope xmlns:demo="http://localhost:8090/demo" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Header/>
    <soapenv:Body>
      <demo:Demo soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
        <param1 xsi:type="xsd:string">abcdefg</param1>
      </demo:Demo>
    </soapenv:Body>
  </soapenv:Envelope>
)
...