1

我正在尝试使用 ruby​​ 库 Savon 发出 SOAP 请求。

我正在使用以下代码:

require "savon"

Savon.configure do |config|
    config.soap_version = 2  # use SOAP 1.2
    config.raise_errors = false
end

wsdl_logon = Savon::Client.new do
    wsdl.document = "https://api.affili.net/V2.0/Logon.svc?wsdl"
end

username = 'XXX'
password = 'YYY'

wsdl_logon.http.headers["Content-Type"] = "text/xml; charset=utf-8"

response = wsdl_logon.request "Logon" do
  soap.body = {'Username' => username, 'Password' => password, 'WebServiceType' => 'Product'}
end

if response.http_error?
    puts "Http Error!"
    puts y response.http_error
else
    puts "No Http Error!"
end

但我不断收到 400 条错误消息(“错误请求”)。或者,如果我删除以下行

wsdl_logon.http.headers["Content-Type"] = "text/xml; charset=utf-8"

我收到 415 错误消息(“不支持的媒体类型”)。

到目前为止,我一直在使用 PHP 发出这些请求,并且以下代码始终可以正常工作:

$soap_logon = new SoapClient('https://api.affili.net/V2.0/Logon.svc?wsdl');
$token = $soap_logon->Logon(array(
    'Username'      => 'XXX',
    'Password'      => 'YYY',
    'WebServiceType'    => 'Product'
));

任何人都可以指出正确的方向,可能的错误来源可能是什么?我现在完全迷路了。

谢谢您的帮助。


我按照 Tom De Leu 的建议做了,并尝试在生成的 SOAP 请求中尽可能多地消除有问题的差异。但我仍然不断收到 400 个错误。对此可能原因的任何提示将不胜感激。

这是 PHP 生成的(工作)请求(为清楚起见,在 XML 中添加了换行符):

POST /V2.0/Logon.svc HTTP/1.1
Host: api.affili.net
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.2.0-8+etch16
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://affilinet.framework.webservices/Svc/ServiceContract1/Logon"
Content-Length: 456

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns1="http://affilinet.framework.webservices/types"
    xmlns:ns2="http://affilinet.framework.webservices/Svc"
>
    <SOAP-ENV:Body>
        <ns2:LogonRequestMsg>
            <ns1:Username>xxx</ns1:Username>
            <ns1:Password>yyy</ns1:Password>
            <ns1:WebServiceType>Product</ns1:WebServiceType>
        </ns2:LogonRequestMsg>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这是 Ruby 生成的(不工作的)请求(再次,为清楚起见添加了 xml 换行符)

SOAP request: https://api.affili.net/V2.0/Logon.svc
Content-Type: text/xml; charset=utf-8, SOAPAction: http://affilinet.framework.webservices/Svc/ServiceContract1/Logon, Content-Length: 605

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:wsdl="http://affilinet.framework.webservices/Svc"
    SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns1="http://affilinet.framework.webservices/types"
    xmlns:ns2="http://affilinet.framework.webservices/Svc"
>
    <SOAP-ENV:Body>
        <ns2:LogonRequestMsg>
            <ns1:Username>XXX</ns1:Username>
            <ns1:Password>YYY</ns1:Password>
            <wsdl:WebServiceType>Product</wsdl:WebServiceType>
        </ns2:LogonRequestMsg>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>


HTTPI executes HTTP POST using the httpclient adapter
SOAP response (status 400):
4

3 回答 3

4

我发现我需要添加标头才能通过 415 错误。

Savon.client(wsdl: "www.sample_doman.com/endpoint?wsdl", headers: {'Content-Type' => 'application/soap+xml; charset=utf-8'})

于 2015-01-09T14:51:26.533 回答
2

我建议查看 PHP 代码发送的 XML,然后将其与 Ruby Savon 代码发送的 XML 进行比较,并检查差异在哪里。然后看看你是否可以修改你的 ruby​​ 代码来生成正确的请求。

于 2011-11-05T13:23:45.137 回答
0

告诉 Savon 使用 SOAP 版本 2(实际上是 1.2),然后手动将内容类型设置为 text/xml 类型会违背目的。

如果您的 Web 服务需要 SOAP 1.2,那么它期望内容类型为“application/soap+xml”,如果您将 soap_version 设置为 2,SAVON 将为您执行此操作。

如果您想要 text/xml 的内容类型,只需将您的 soap_version 配置变量设置为 1

于 2014-06-23T18:14:32.817 回答